使用javascript拦截所有文档链接点击 [英] use javascript to intercept all document link clicks
本文介绍了使用javascript拦截所有文档链接点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何拦截文档中的链接点击?它必须是跨平台的.
how do I intercept link clicks in document? it must be cross-platform.
我正在寻找这样的东西:
I am looking for something like this:
// content is a div with innerHTML
var content = document.getElementById("ControlPanelContent");
content.addEventListener("click", ContentClick, false);
function ContentClick(event) {
if(event.href == "http://oldurl")
{
event.href = "http://newurl";
}
}
预先感谢您的帮助.
推荐答案
for (var ls = document.links, numLinks = ls.length, i=0; i<numLinks; i++){
ls[i].href= "...torture puppies here...";
}
或者,如果您只想拦截而不是更改,请添加一个 onclick 处理程序.这将在导航到 url 之前被调用:
alternatively if you just want to intercept, not change, add an onclick handler. This will get called before navigating to the url:
var handler = function(){
...torment kittens here...
}
for (var ls = document.links, numLinks = ls.length, i=0; i<numLinks; i++){
ls[i].onclick= handler;
}
请注意,document.links
还包含带有 href
属性的 AREA
元素 - 而不仅仅是 A
元素.
Note that document.links
also contains AREA
elements with a href
attribute - not just A
elements.
这篇关于使用javascript拦截所有文档链接点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文