防止jQuery.fn.extend多次触发
Prevent jQuery.fn.extend from firing multiple times
我举了以下示例
<script>
$( "#buttom1" ).click(function() {
console.log('ok')
$.fn.thefuncion();
});
$.fn.thefuncion=function(){
jQuery('#buttom2').click(function(){
alert('ready')
});
}
</script>
<input type="submit" name="button" id="buttom1" value="buttom 1">
<input type="submit" name="button" id="buttom2" value="buttom 2">
问题是警报出现的次数是按下1按钮的次数
无论按下1按钮的次数如何,我都需要在按下按钮2后仅显示警报。
试着用正确的方式来做。不要多次绑定事件。这是没有必要的。你可以通过活动授权的形式来实现你想要的。
$( "#buttom1" ).click(function() {
jQuery('#buttom2').addClass("active");
});
jQuery(document).on('click','.active', function(){
alert('ready')
});
其中document
将是事件冒泡的终点,由此触发事件的罪犯将被抓住,其事件将被触发(如果有的话)。因此,不需要遍历到文档。代替document
传递按钮buttom2
的任何静态最近父级
演示
您可以使用jQuery .one()
函数:
$( "#buttom1" ).one('click', function() {
console.log('ok')
$.fn.thefuncion();
});
此事件将在第一次单击按钮1时触发,按钮2事件将只设置一次。
相关文章:
- 使用带有自定义对象作为属性的jQuery.extend时出现意外行为
- jQuery extend 函数是怎么回事
- 使用 YUI 3 跟踪 jQuery.extend
- 我什么时候应该在jQuery中使用$.fn.extend()
- JQuery .extend() and Javascript class
- jQuery.extend 抛出带有自引用的堆栈溢出异常
- jQuery.extend() deep clone nested objects
- jQuery ui Datepicker - global extend/override?
- jquery extend for i18n
- 使用 fn.extend() 和 Google 闭包编译器扩展 jQuery
- 防止jQuery.fn.extend多次触发
- jQuery$.extend名称空间
- 在使用jquery$.extend()时忽略属性
- jQuery$.extend,而不覆盖目标属性
- 理解jQuery$.extend()的正确用法
- 使用$extend()为jQuery中的switch语句函数添加更多事例
- 为什么jQuery.extend()比Lodash.clone()快
- 在jQuery.extend()中使用$(this)
- jQuery: object[property].push()在使用jQuery.extend()创建新对象时修改旧对象
- jquery.Extend (true, [], obj)不创建深度拷贝