考虑following code:.click(...)和.live('click',...)之间有什么区别?
HTML:
<div id='button' class='enabled'>Press here</div>
<div id='log'></div>
CSS:
#button {
width: 65px;
height: 25px;
background-color: #555;
color: red;
padding: 10px 20px;
}
#button.enabled {
color: #333;
}
#button.enabled:hover {
color: #FFF;
cursor: pointer;
}
的JavaScript:
$(function() {
$('#button.enabled').live('click', function() { // (1)
//$('#button.enabled').click(function() { // (2)
log('#button.enabled clicked');
});
});
function log(str) {
$('#log').append(str + '<br />');
$('#button').toggleClass('enabled');
}
此代码正常工作,即log()
被称为只有当单击0按钮。
但是,如果我将(1)
替换为(2)
,则log()
也会在不单击enabled
按钮时被调用。
这是为什么? (1)
和(2)
有什么区别?
非常感谢Nick!你是最棒的 !! – 2010-09-06 12:13:22
@Misha - welcome :)希望能够清楚地说明这一点,对于许多* jQuery用户来说,这绝对是一个混乱点。 – 2010-09-06 12:16:18