覆盖链接上的CTRL +单击行为

时间:2014-08-01 15:17:40

标签: javascript html

我需要覆盖crtl +点击HTML或Javascript中的链接。我希望这样当用户进行crtl +点击链接时,而不是执行浏览器的默认行为,而是执行我的功能。我无法使用jQuery来执行此操作。有什么想法吗?

这是我想要调用的函数:

function alertMe()
{
alert('overwrote the crtl click');
}

1 个答案:

答案 0 :(得分:3)

捕获点击事件,然后使用event.ctrlKey

查看是否按下了控制按钮
document.getElementById("datAnchor").addEventListener("click", function(e) {
    if (e.ctrlKey) {
        //your code here
    }
    // else do nothing with e.preventDefault();
}, false);

如果你的意思是所有的锚标签,那么你必须对选择器有所了解并把它扔进一个像...这样的循环中。

Array.prototype.forEach.call(document.getElementsByTagName("a"), function(element) {
    //code from other sample in here, except replace the
    //getElementById with `this`
});

对于活动爱好者选择者,document.querySelectorAll应该适合您的利益。