English 中文(简体)
在页脚中使用 jQuery
原标题:Using jQuery in footer

总是建议(例如,见 this )将JS链接放在页脚中。

include  header.php 
Page Content
include  footer.php 

我们在某个页面的 < code> Page 内容 中添加自定义 jQuery 代码, 在页脚上加载 jQuery 库。 这将引起问题, 因为 jQuery 库必须在 jQuery 代码之前加载 。

“强度问题1:

一种可能的解决办法是将所有 jQuery 代码( 所有网页) 都包含在页脚中。 在这种情况下, 所有的 jQuery 代码都可以添加到 JUS 文件中, 而不是内嵌 JavaScript 。 这将类似 。

$(document).ready(function(){
$( .div1 ).click ....
$( .div2 ).click ....
.....
$( .div99 ).click ....

问题2: " /强 " 是否减缓了JavaScript进程,如同每页一样,联署材料必须听取当前页面中没有的DOM元素可能发生的情况?例如,联署材料必须照顾99个DIVs,而每页则有一些DIVs。

问题回答

Answer to Question 1 Standard practice is to place large scripts in its own file and have the script tag in the header. As long as you re calling $(document).ready you re not incurring any hits to performance as that script will only run when the document is loaded.

将联署材料放在页脚的用意是,在整页装满后执行,但随着人员上载事件,这已不再必要。

如果您真的打算将JS留在页脚中, 创建带有您要装入的脚本名称的页面变量, 并将其传递到您的脚脚脚脚本, 任务很简单 。

Answer to Question 2 No, the page isn t slowed down. If jquery doesn t find a matching tag, it does not bind any event. Even if there were 99 divs on the page, it s not a performance hit, because the events are stored by reference, and only the invoked DOM item and its event will be fired.

最好从一个文件上尽快加载JUS,

@samsaffron写了一篇相当明确的文章-http://samsaffron.com/archive/2012/02/17/stop-pay-your-jquery-tax" rel=“no follow”>http://samsaffron.com/archive/2012/02/17/stop-pay-our-jquery-tax





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签