本文介绍了使用JavaScript截取所有文件链接点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何拦截文档中的链接点击?
它必须是跨平台的。



我正在寻找这样的东西:



<$ p $内容是innerHTML的div
var content = document.getElementById(ControlPanelContent);

content.addEventListener(click,ContentClick,false);

函数ContentClick(event){

if(event.href ==http:// oldurl)
{
event.href =的 http:// NEWURL;




$ b

感谢您的帮助。 b $ b

解决方案
  for(var ls = document.links,numLinks = ls.length,i = 0; i< numLinks; i ++){
ls [i] .href =...酷刑小狗在这里...;

code


$ b

或者,如果你只是想拦截,而不是改变,添加一个onclick处理程序。这会在导航到url之前被调用:

  var handler = function(){
...折磨小猫这里...

for(var ls = document.links,numLinks = ls.length,i = 0; i< numLinks; i ++){
ls [i] .onclick =处理程序;

$ / code>

请注意 document.links 还包含具有 href 属性的 AREA 元素 - 而不仅仅是 A 元素。


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";
  }
}

Thanks in advance for help.

解决方案
for (var ls = document.links, numLinks = ls.length, i=0; i<numLinks; i++){
    ls[i].href= "...torture puppies here...";
}

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;
}

Note that document.links also contains AREA elements with a href attribute - not just A elements.

这篇关于使用JavaScript截取所有文件链接点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 18:43