Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当我的站点上的任何链接(a)被点击时,我如何运行jQuery函数_Javascript_Jquery_Function - Fatal编程技术网

Javascript 当我的站点上的任何链接(a)被点击时,我如何运行jQuery函数

Javascript 当我的站点上的任何链接(a)被点击时,我如何运行jQuery函数,javascript,jquery,function,Javascript,Jquery,Function,我有一个新的网站建立在corecommerce系统上,它没有太多访问HTML和非PHP的权限。我唯一能用的就是JavaScript。他们的系统目前页面加载速度不是很快,所以我希望客户在等待5-8秒加载页面时,至少知道发生了什么。所以我找到了一些代码片段,并将它们放在一起,在加载页面时显示一个加载GIF的覆盖图。当前,如果您单击页面上的任何位置,它将运行,但我希望它仅在单击站点上的链接(a href)(任何链接)时运行 我知道你可以在页面加载时运行一段代码,但这还不够好,因为它执行得太晚(几秒钟后

我有一个新的网站建立在corecommerce系统上,它没有太多访问HTML和非PHP的权限。我唯一能用的就是JavaScript。他们的系统目前页面加载速度不是很快,所以我希望客户在等待5-8秒加载页面时,至少知道发生了什么。所以我找到了一些代码片段,并将它们放在一起,在加载页面时显示一个加载GIF的覆盖图。当前,如果您单击页面上的任何位置,它将运行,但我希望它仅在单击站点上的链接(a href)(任何链接)时运行

我知道你可以在页面加载时运行一段代码,但这还不够好,因为它执行得太晚(几秒钟后)

无论如何,这是我的网站www.cosmeticsbynature.com,这是我使用的代码。任何帮助都会很好

<div id="loading"><img src="doen'tallowmetopostanimage" border=0></div>


<script type="text/javascript">
var ld=(document.all);
var ns4=document.layers;
var ns6=document.getElementById&&!document.all;
var ie4=document.all;
  if (ns4)
 ld=document.loading;
 else if (ns6)
 ld=document.getElementById("loading").style;
 else if (ie4)
 ld=document.all.loading.style;
jQuery(document).click(function()
{
if(ns4){ld.visibility="show";}
else if (ns6||ie4)
var pb = document.getElementById("loading");
pb.innerHTML = '<img src="http://www.cosmeticsbynature.com/00222-1/design/image/loading.gif" border=0>';
ld.display="block";
});
</script>

var ld=(document.all);
var ns4=document.layers;
var ns6=document.getElementById&!所有文件;
var ie4=document.all;
如果(ns4)
ld=文件加载;
否则,如果(ns6)
ld=document.getElementById(“加载”).style;
否则如果(ie4)
ld=document.all.load.style;
jQuery(文档)。单击(函数()
{
如果(ns4){ld.visibility=“show”;}
否则如果(ns6 | ie4)
var pb=document.getElementById(“加载”);
pb.innerHTML='';
ld.display=“block”;
});

您所做的是将单击处理程序绑定到文档,因此无论用户在何处单击将执行的代码,都要更改这段代码

jQuery(document).click(function()


注意:
.on()
在jQuery 1.7中是新的。

如果在页面中包含jQuery,那么这样做会更容易。完成后,您可以执行以下操作:

$('a').click(function() {
 // .. your code here ..
 return true; // return true so that the browser will navigate to the clicked a's href
}
这个怎么样?我假设#加载{display:none}

<div id="loading"><img src="http://www.cosmeticsbynature.com/00222-1/design/image/loading.gif" border=0></div>
<script type="text/javascript">
document.getElementById('loading').style.display='block'; // show the loading immediately
window.onload=function() 
  document.getElementById('loading').style.display='none'; // hide the loading when done
}
</script>

document.getElementById('loading').style.display='block';//立即显示装载情况
window.onload=function()
document.getElementById('loading').style.display='none';//完成后隐藏加载
}

我在给出的大多数答案中看到的一个问题是,人们认为点击事件只来自
(锚定)标记。在我的实践中,我经常将单击事件添加到
span
li
标记中。给出的答案没有考虑这些因素

下面的解决方案嗅探包含这两个事件的元素,这些事件是使用
jQuery创建的。单击(function(){})


使用上述方法,您可能希望创建一个数组并将找到的元素推送到数组中(你看到的每个地方
console.log

另一个问题是盲目地回答问题,而不试图解决实际问题…我不确定OP是否真的需要一个jQuery解决方案来单击任何东西你是对的,但是这个答案,同时使用jQuery对它的一部分进行查询(主要用于循环对象),使用基本JavaScript解决我描述的问题(非锚链接)if(this.onclick&&this.onclick.toString()!=“”){…做需要的事…}是解决方案的核心,而不是jQuery
//to select all links on a page in jQuery
jQuery('a')

//and then to bind an event to all links present when this code runs (`.on()` is the same as `.bind()` here)
jQuery('a').on('click', function () {
    //my click code here
});

//and to bind to all links even if you add them after the DOM initially loads (`on()` is the same as `.delegate()` here; with slightly different syntax, the event and selector are switched)
jQuery(document).on('click', 'a', function () {
    //my click code here
});
$('a').click(function() {
 // .. your code here ..
 return true; // return true so that the browser will navigate to the clicked a's href
}
<div id="loading"><img src="http://www.cosmeticsbynature.com/00222-1/design/image/loading.gif" border=0></div>
<script type="text/javascript">
document.getElementById('loading').style.display='block'; // show the loading immediately
window.onload=function() 
  document.getElementById('loading').style.display='none'; // hide the loading when done
}
</script>
$(document).ready(function(){

    // create jQuery event (for test)
    $('#jqueryevent').click(function(){alert('jqueryevent');});

    // loop through all body elements
    $('body *').each(function(){

        // check for HTML created onclick
        if(this.onclick && this.onclick.toString() != ''){
           console.log($(this).text(), this.onclick.toString());
        }

        // jQuery set click events
        if($(this).data('events')){           
            for (key in($(this).data('events')))
                if (key == 'click')
                   console.log( $(this).text()
                              , $(this).data('events')[key][0].handler.toString());
        }
   });
});