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.

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

我已经阅读了jQuery的错误报告,其中介绍了如果语句中发生异常,.ready事件将如何触发两次.但是,即使我有以下代码,它仍然运行两次:

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

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?

Here is the head block for the site:

<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函数)

推荐答案

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>标记和not之前使用$('body').wrapInner();包含脚本标记来避免.使用$('body').html($('body').html().replace(...));也有同样的效果.

Jquery相关问答推荐

在shiny 的datatable列中启用智能搜索

在 JQuery DataTable 中通过按钮单击传递 ID

Sheets从多张sheet中导入range匹配数据

jQuery非只读 Select 器

将一个类动态添加到 Bootstrap 的popover容器中

如何 Select 具有特定文本的所有锚标记

如何同时使用 requireJS 和 jQuery?

jQuery 判断 是否存在并且有一个值

如何使所有浏览器都支持 ?有什么 Select 吗?

如何更改 dropzone.js 中的默认文本?

使用 jQuery 从 AJAX 响应(json)构建表行

jQuery:获取父母,父母ID?

为什么使用 jQuery on() 而不是 click()

在不丢失光标位置的情况下更新输入值

如何使用 jQuery 停止默认链接点击行为

延迟jquery悬停事件?

jQuery: Select 所有具有自定义属性的元素

如何使用 jQuery 检测 IE 8?

如何确定滚动高度?

jquery追加到列表的前面/顶部