[英]One event on several elements
I have this function: 我有这个功能:
function Any(){
this.b1 = $("#but1")
this.b2 = $('<div id="but2" />')
this.func = function(){
alert('hello')
}
this.b1.on('click',function(){
this.func()
})
this.b2.on('click',function(){
this.func()
})
}
I want to do something like this: 我想做这样的事情:
(this.b1, this.b2).on('click',this.close)
I would keep an array of selectors: 我会保留一个选择器数组:
//code
this.selectors = ["#but1", "#but2"];
$(this.selectors.join(",")).on('click', this.close);
//code
$("#but1,#but2").on("click",function(){
this.func();
});
Using the advanced syntax of .on() you may attach a listener to 使用.on()的高级语法,您可以将侦听器附加到
a defined and stable object/selector (this) to look for the even on 一个定义且稳定的对象/选择器(此)以寻找偶数
dynamic SubComponents/selectors (#but1, #but2) 动态子组件/选择器(#but1,#but2)
witch may or may not exists ... or show up later. 女巫可能存在或可能不存在...或稍后出现。
this.on('click','#but1,#but2',this.close);
Or in the more general case: 或者在更一般的情况下:
document.on('click','#but1,#but2', function(){ $('#my_dialog').close});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.