JQuery:放置HTML标记处理程序的最佳位置 [英] JQuery: best place to put html tag handlers

查看:67
本文介绍了JQuery:放置HTML标记处理程序的最佳位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您个人更喜欢将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屋!

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