检测单击的链接的 ID 以更改链接的去向
Detect the ID of a clicked link to change where the link goes?
所以我正在尝试制作一个名为testBrowser()
的JavaScript函数来检测用户是否在移动设备上(我有这个工作)。
我有三个链接运行相同的功能,但我希望每个链接都有不同的结果。我已经找到了我想做的方法,但实际上无法做到,因为我需要检测单击的链接的 ID。我在网上找到的所有选项似乎都不起作用!
我正在努力解释这一点,但阅读我的代码应该会有所帮助。
.HTML:
<ul>
<li><a href="javascript:testBrowser()" id="tumblr">z</a></li>
<li><a href="javascript:testBrowser()" id="twitter">a</a></li>
<li><a href="javascript:testBrowser()" id="insta">x</a></li>
</ul>
JavaScript:
function testBrowser(){
if(/Android|webOS|iPhone|iPod/BlackBerry/i.test(navigator.userAgent)){
if($(this).attr('id') = "insta"){
window.location = "instagram://user?id=xxxxxxxx";
}
else if($(this).attr('id') = "tumblr"){
window.location = "tumblr://x-callback-url/blog?blogName=blogname";
}
else if($(this).attr('id') = "twitter"){
window.location = "twitter://user?screen_name=handle";
}
}
else{
if($(this).attr('id') = "insta"){
window.location = "http://www.instagram.com/id";
}
else if($(this).attr('id') = "tumblr"){
window.location = "http://blogname.tumblr.com/";
}
else if($(this).attr('id') = "twitter"){
window.location = "http://www.twitter.com/handle";
}
}
}
我做错了什么?!
JSFiddle
以这种方式从href
调用不会返回正确的this
引用(它将引用window
而不是anchor
)。您可以通过编程方式向链接添加单击函数。jQuery可以在一行中做到这一点。这实际上是首选方式。
$("li > a[id]").on("click", testBrowser);
这将选择锚点并将testBrowser
添加为函数。
其他改进:
- 您在 if 子句中使用了单
=
,它们需要是双==
或三===
才能进行比较。 - 您的正则表达式包含一个错误,其中
/
需要是|
。 - 我将
hrefs
更改为void(0)
,因此它们仍然充当链接,但实际上什么都不做。
$("li > a[id]").on("click", testBrowser);
function testBrowser(){
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
if($(this).attr('id') == "insta"){
window.location = "instagram://user?id=xxxxxxxx";
}
else if($(this).attr('id') == "tumblr"){
window.location = "tumblr://x-callback-url/blog?blogName=blogname";
}
else if($(this).attr('id') == "twitter"){
window.location = "twitter://user?screen_name=handle";
}
}
else{
if($(this).attr('id') == "insta"){
window.location = "http://www.instagram.com/id";
}
else if($(this).attr('id') == "tumblr"){
window.location = "http://blogname.tumblr.com/";
}
else if($(this).attr('id') == "twitter"){
window.location = "http://www.twitter.com/handle";
}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li><a href="javascript:void(0)" id="tumblr">z</a></li>
<li><a href="javascript:void(0)" id="twitter">a</a></li>
<li><a href="javascript:void(0)" id="insta">x</a></li>
</ul>
当前调用testBrowser
的方式在全局对象上下文中执行它,即 window
.您可以像这样更改 HTML:
<a href="javascript:testBrowser.call(this)" target="_blank" class="socicon" id="tumblr">z</a>
上。阿博贝行不通。它应该是onclick="testBrowser.call(this)"
属性,上下文本身href="javascript:
全局对象,所以call(this)没有意义。jQuery解决方案要好得多。
在这种情况下,您只需使用 this.id
访问链接id
。无需用$()
包裹它.
问题#2。使用比较运算符===
而不是赋值=
:
if (this.id === "insta") {
window.location = "instagram://user?id=xxxxxxxx";
}
相关文章:
- 根据选项卡内部的文本链接中的哈希ID动态选择jqueryUI选项卡
- 如何进行AJAX调用,使其仅在我单击包含在特定类中的链接时触发;id”;父元素的
- 通过单击链接(兄弟姐妹?)从无线电输入中获取id
- 通过id查找元素并附加链接
- url中没有id的内部链接
- 如何绑定多个具有相同敲除功能、传递不同ID的链接
- 修复Javascript代码以将文本中的所有Youtube链接转换为Youtube ID功能
- 如何从链接中检索 ID
- 使用 JS 捕获链接 ID,然后定向到链接
- 如何在另一个html文件中获取超链接ID
- jQuery编辑链接id
- 链接id使用jquery传递值到表单
- 使用javascript替换基于查询字符串的自定义链接id
- 基于它's调用超链接id定制弹出窗口
- 文档.getElementById用于链接id
- 从jQuery选项卡搜索脚本中删除链接ID元素
- 从jQuery选项卡脚本的链接ID中删除元素
- 简单的JavaScript将数组链接ID附加到html文档中
- 提取要在 jquery 函数中使用的链接 ID
- 使用javascript获取被点击的html链接id