简体   繁体   English

<script> tag at bottom causing $(document).ready to fail

[英]<script> tag at bottom causing $(document).ready to fail

I was following the snippets from html5boilerplate.com (as well as something that is recommended by yahoo) that we should put the scripts at the bottom of the page (of my site's main page). 我遵循的是html5boilerplate.com(以及yahoo推荐的内容)中的代码片段,我们应该将脚本放在页面底部(我网站的主页)。

I am also following SPI (Single page interface) - meaning I will load the content area only via ajax for any new page visit. 我也遵循SPI(单页面界面)-这意味着我将仅通过ajax加载内容区域以进行任何新页面访问。

My page structure is.. 我的页面结构是..

<body>
<header />
<div id="content" />
<footer />
</body>

Now issue is .. I load JQuery at the end of main page. 现在的问题是..我在主页末尾加载了JQuery。 But I need some javascript to get executed on the child page which would be loaded in content div. 但是我需要一些JavaScript在子页面上执行,该子页面将在content div中加载。 So if I write $(document).ready(..) in the child page, it will crib about "$ is not defined". 因此,如果我在子页面中写入$(document).ready(..) ,它将显示“未定义$”。

Any suggestions? 有什么建议么?

Put your jquery include into your header. 将您的jquery包含在标头中。 Then it will work. 然后它将起作用。 As long you don't need a very high performance site you don't need such performance hacks. 只要您不需要非常高性能的站点,就不需要此类性能黑客。 And you should understand how and why the hacks work before using them. 而且,在使用这些技巧之前,您应该了解这些技巧的工作方式和原因。 If you really want to include jquery at the bottom of the script you have to put all your JS code below the jquery include. 如果您确实想在脚本的底部包含jquery,则必须将所有JS代码放在jquery include的下面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM