javascript - 如何在 JQuery/Javascript 中检测点击的 href URL

标签 javascript jquery

有谁知道如何在 JQuery 中检测点击的 href URL?例如我有以下链接,有些是重定向到新页面,有些会调用 javascript 来扩展 div。如果没有使用 ID,我如何使用 JQuery/Javascript 检测 href。

<a href="www.test.com">Test 1</a>
<a href="javascript:test()">Test 2</a>
<a href="www.test2.com">Test 3</a>
<a href="www.test3.com">Test 4</a>
<a href="javascript:test2()">Test 5</a> 

最佳答案

Does anyone know how to detect the clicked href URL in JQuery?

$("a").click(function(event){
    // Capture the href from the selected link...
    var link = this.href;

    alert(link);

    // do something if required....

    // would prevent the link from executing, if that is something you want to do...
    return false; // not needed if you want the link to execute normally...
});

关于javascript - 如何在 JQuery/Javascript 中检测点击的 href URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12468247/

相关文章:

javascript - 限制可选择的 DOM 复选框

javascript - 单击图像时,图像列表中的同一图像以模态显示

javascript - 弹出窗口也会出现在页面加载时。我无法弄清楚错误

javascript - 如何使用 jQuery 在不同的端口上发送 AJAX 请求?

javascript - 处理大型 JavaScript 应用程序

javascript - 使用 JSON.parse 评估 Javascript 数据结构时出错

javascript - 我如何在 Aurelia 中使用 Promises?

javascript - JQuery 模态对话框多重提交

jquery(selector) 如何从两个不同的类中选择最后一个元素

javascript - 带有图像和长文本的 Bootstrap 下拉菜单