单击网站上的任何链接(a)时如何运行jQuery函数 [英] How do I run a jQuery function when any link (a) on my site is clicked

查看:117
本文介绍了单击网站上的任何链接(a)时如何运行jQuery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在corecommerce系统上建立了一个新站点,该站点对HTML的访问不多,对PHP的访问不多.我只能使用JavaScript.他们的系统当前页面加载速度并不理想,所以我希望至少客户在等待5-8秒以加载页面时知道发生了什么.因此,我找到了一些代码,并将它们放在一起,以显示页面加载时叠加GIF的方式.当前,如果您单击页面上的任意位置,它将运行,但是我希望仅当单击站点上的链接(任何链接)时才运行它.

I have a new site build on corecommerce system which does not have much access to HTML and non to PHP. Only thing I can use is JavaScript. Their system is currently not great on page load speed so I wanted at least customers to know something is happening while they wait 5-8 seconds for a page to load. So I found some pieces of code and put them together to show an overlay loading GIF while page is loading. Currently it will run if you click anywhere on the page but I want it to run only when a link (a href) on the site is clicked (any link).

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

I know you can do a code that will run while page loading but this isn't good enough as it will execute too late (after few seconds)

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

Anyway, this is my website www.cosmeticsbynature.com and this is the code I use. Any help will be great.

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

推荐答案

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

Doing this will be easier if you include jQuery in your pages. Once that is done, you can do:

$('a').click(function() {
 // .. your code here ..
 return true; // return true so that the browser will navigate to the clicked a's href
}

这篇关于单击网站上的任何链接(a)时如何运行jQuery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆