jQuery $(document).ready()触发两次 [英] jQuery $(document).ready () fires twice

查看:155
本文介绍了jQuery $(document).ready()触发两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在网上搜索,试图找出这里发生了什么,我无法得到具体答案。

So i've been sifting around the web trying to find out whats going on here and I have not been able to get a concrete answer.

我有一个$(document).ready在我的网站上,无论内部代码如何都会连接多次运行。

I have one $(document).ready on my site that seams to run multiple times regardless of the code that is inside it.

我已经阅读了jQuery的bug报告关于如果你的语句中有异常,.ready事件将如何触发两次。但即使我有以下代码,它仍会运行两次:

I've read up on the bug reports for jQuery about how the .ready event will fire twice if you have an exception that occurs within your statement. However even when I have the following code it still runs twice:

$(document).ready(function() {
    try{    
        console.log('ready');
        }
    catch(e){
        console.log(e);
    }
});

在控制台中我看到的所有内容都已准备好记录两次。是否有可能另外一个有例外的问题会引起问题?我的理解是所有.ready标签都是相互独立的,但我似乎无法找到它的起源?

In the console all I see is "ready" logged twice. Is it possible that another .ready with an exception in it would cause an issue? My understanding was that all .ready tags were independent of each other, but I cannot seem to find where this is coming into play?

这是网站的主要部分:

<head>
<title>${path.title}</title>
<meta name="Description" content="${path.description}" />
<link href="${cssHost}${path.pathCss}" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript" charset="utf-8"><!----></script>
<script src="media/js/fancybox/jquery.fancybox.pack.js" type="text/javascript" ><!-- --></script>
<script src="/media/es/jobsite/js/landing.js" type="text/javascript" ><!-- --></script>
<script src="/media/es/jobsite/js/functions.js" type="text/javascript"><!-- -->    </script>
<script src="/media/es/jobsite/js/jobParsing.js" type="text/javascript" charset="utf-8"><!----></script>
<script src="/media/es/jobsite/js/queryNormilization.js" type="text/javascript" charset="utf-8"><!----></script>
<script src="${jsHost}/js/jquery/jquery.metadata.js" type="text/javascript" charset="utf-8"><!----></script>
<script src="${jsHost}/js/jquery/jquery.form.js" type="text/javascript" charset="utf-8"><!----></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.7/jquery.validate.min.js" type="text/javascript" charset="utf-8"><!----></script>
<script src="${jsHost}/js/jquery.i18n.properties-min.js" type="text/javascript" charset="utf-8"><!----></script>

<script type="text/javascript" charset="utf-8">

function updateBannerLink() {
    var s4 = location.hash.substring(1);
    $("#banner").attr('href','http://INTELATRACKING.ORG/?a=12240&amp;c=29258&amp;s4='+s4+'&amp;s5=^');
}

</script>
</head>

不要关注JSP变量,但正如你所看到的那样我只调用函数。 js文件一次(这是.ready函数存在的地方)

Pay no attention to the JSP variables, but as you can see i'm only calling the functions.js file once (which is where the .ready function exists)

推荐答案

ready事件无法触发两次。更可能发生的是你有代码移动或操纵包含代码的元素,导致浏览器重新执行脚本块。

The ready event cannot fire twice. What is more than likely happening is you have code that is moving or manipulating the element that the code is contained within which causes the browser to re-execute the script block.

这可以通过在< head> 中或在结束< / body> 标记之前包含脚本标记来避免和使用 $('body')。wrapInner(); 。使用 $('body')。html($('body')。html()。replace(...)); 具有相同的效果。

This can be avoided by including script tags in the <head> or before the closing </body> tag and not using $('body').wrapInner();. using $('body').html($('body').html().replace(...)); has the same effect.

这篇关于jQuery $(document).ready()触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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