off() 正在删除点击事件句柄。
on() 应该重新绑定(bind)效果,但不起作用
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('p').on('click', function(){
$(this).css('color', 'blue');
});
$('#btn_off').click(function(){
$('p').off('click');
});
$('#btn_on').click(function(){
$('p').on('click');
});
});
</script>
<span>Click the paragraph:</span>
<p>Prg #1</p>
<p>Prg #2</p>
<p>Prg #3</p>
<p>Prg #4</p>
<button id="btn_off">bind off prg.</button>
<button id="btn_on">bind on prg.</button>
请参阅标题中的我的问题。
最佳答案
您在绑定(bind)事件时没有传递点击处理程序。
使用
$(document).ready(function(){
//Declare click handler
var pClickHandler = function(){
$(this).css('color', 'blue');
}
//Bind click handler on document ready
$('p').on('click', pClickHandler);
$('#btn_on').click(function(){
//Bind click handler on on click
$('p').on('click', pClickHandler);
});
});
关于jquery - 去掉点击事件off()后如何使用on()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28647390/