jQuery:element.click(func)或element.attr('onclick','func()')效率更高吗? [英] jQuery: is element.click(func) or element.attr('onclick','func()') more efficient?
问题描述
我正在通过将元素克隆到列表中来填充列表.然后,我更改属性以使每个项目都唯一.他们需要在点击时调用一个函数,所以我想知道使用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屋!