我有一个新的网站建立在corecommerce系统,这是没有太多的访问HTML和非PHP。我唯一能用的就是JavaScript。他们的系统目前在页面加载速度上不是很好,所以我至少希望客户在等待页面加载5-8秒时知道发生了什么事情。因此,我找到了一些代码片段,并将它们放在一起,以显示页面加载时的覆盖加载GIF。目前,它将运行,如果你点击页面上的任何地方,但我希望它只运行时,在网站上的一个链接(一个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>发布于 2011-12-17 01:51:56
如果在页面中包含jQuery,这样做会更容易。完成后,您可以执行以下操作:
$('a').click(function() {
 // .. your code here ..
 return true; // return true so that the browser will navigate to the clicked a's href
}发布于 2011-12-17 01:51:22
//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
});注意:.on()是jQuery 1.7中的新特性。
发布于 2011-12-17 01:50:21
您所做的就是将click处理程序绑定到文档,这样,无论用户在哪里单击,都会执行代码,更改这段代码
jQuery(document).click(function()至
jQuery("a").click(function()https://stackoverflow.com/questions/8538101
复制相似问题