JQuery:放置HTML标记处理程序的最佳位置 [英] JQuery: best place to put html tag handlers
问题描述
您个人更喜欢将html标记处理程序放入使用JQuery驱动的Html页面,以及为什么:html标记元素或document.ready函数?
< a onclick =javascript:foo()/>
...
函数foo(){
...
}
或
< a id =mytag/> ;
$(document).ready(function(){
$(#mytag)。click(function(){.. 。});
})
预先感谢您!
处理 $(document).ready
更好。 1)代码使用本地,而不是全局功能。它很快,您可以访问 $(document).ready
函数中声明的局部变量2)您确定 onclick
函数在用户点击之前加载。 3)从设计的角度来看它更好。
Where do you personally prefer to put html tag handlers with JQuery-powered Html page and why: html tag element or document.ready function ?
<a onclick="javascript:foo()" />
...
function foo() {
...
}
or
<a id="mytag" />
...
$(document).ready(function() {
$("#mytag").click(function() { ... });
})
Thank you in advance!
The version with the handler inside of $(document).ready
is better. 1) the code use local and not global functions. It is quickly and you can access to local variables declared inside of $(document).ready
functions 2) you are sure that the code of onclick
function is loaded before the user able to click it. 3) It is better from the design point of view.
这篇关于JQuery:放置HTML标记处理程序的最佳位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!