JQuery set the same event for several selectors
As opposed to this question, here I am asking if it's possible to do something like this:
$(several different selectors).click(function() {
console.log("Click");
});
So if I have something like this:
<div id="a">A</div>
<div id="b">B</div>
<div id="c">C</div>
I want to make several different selectors
select #a
, #b
and #c
, and then whichever one I click would log "Click". I tried $('#a', "#b", "#c")
and it doesn't work; can someone help?
传递一个字符串,并用逗号分隔选择器:
$('#a, #b, #c').click(function() {
console.log("Click");
});
你可以试试这个,因为我已经试过了,它对我很有用
$(document).ready(function () {
$('#a, #b, #c').click(function () {
console.log("Click");
});
});
Your attempt was very close. Here is the documentation multiple selectors.
Your selector should be $("#a, #b, #c")
上一篇: 将对象传递给jQuery.on()
下一篇: JQuery为几个选择器设置了相同的事件