jQuery:element.click(func)或element.attr('onclick','func()')效率更高吗? [英] jQuery: is element.click(func) or element.attr('onclick','func()') more efficient?

查看:136
本文介绍了jQuery:element.click(func)或element.attr('onclick','func()')效率更高吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过将元素克隆到列表中来填充列表.然后,我更改属性以使每个项目都唯一.他们需要在点击时调用一个函数,所以我想知道使用new_element.click(func);new_element.attr('onlick','func();');

I'm populating a list by cloning elements into it. Then I change attrs to make each item unique. They need to call a function on click, so I'm wondering if it's more efficient to use new_element.click(func); or new_element.attr('onlick','func();');

推荐答案

new_element.attr('onclick','func();');

是:

  • 效率低下(不必要地从字符串创建新的内联函数,除了调用func并失去this引用外,什么都不做);

  • inefficient (needlessly creating a new inline function from a string, that does nothing except call func and lose the this reference);

加重放置任何复杂的代码,因为所有代码都必须转义为JS字符串;

aggravating to put any complex code in, since it all has to be JS string escaped;

由于setAttribute中的错误而在IE中损坏.

broken in IE, due to bugs in setAttribute.

避免. click()/bind('click')在这里是有原因的.

Avoid. click()/bind('click') is there for a reason.

这篇关于jQuery:element.click(func)或element.attr('onclick','func()')效率更高吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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