开发者

can i have a multi class selector in jquery

开发者 https://www.devze.com 2023-01-14 14:33 出处:网络
Can I do something like the below where I can have multiple classes trigger an event? $(\'a.red a.blue a.green\').click(function(event) 开发者_运维百科

Can I do something like the below where I can have multiple classes trigger an event?

$('a.red a.blue a.green').click(function(event) 开发者_运维百科
{

});


Yes, you can use the comma as separator.

$('a.red, a.blue, a.green').click(function(event) {});


A slightly different approach would be to prefix your classes like a.prefix_red, a.prefix_blue, a.prefix_green and then use a wildcard selector on it like:

$("a[class^=prefix_]")

The advantage is that as long as you prefix all your "trigger" classes, you don't have to edit the jQuery every time you add a new one, not that it would be a major edit anyways, but might come in handy if you decide to minify your script for example.


$('a.red.blue.green') selects the elements that have all those classes.

$('a.red, a.blue, a.green') selects the elements that have atleast one of those classes.

0

精彩评论

暂无评论...
验证码 换一张
取 消