How can I combine two jQuery selectors?

2019-01-20 19:37发布

问题:

I have the following selectors:

$('#menu a[href^="/C"]').live('click', function (event) { } )
$('#menu a[href^="/T"]').live('click', function (event) { } )

One selects href that starts with /C and the other /T

Is there a way that I can combine these?

回答1:

The selectors you gave were exactly the same, but in general all you have to do is comma delimit the two selectors like the following:

$('#firstSelector, #secondSelector');

For more information check out the jquery site on mutiple selectors

Edit:

This should be what you are looking for:

$('#menu a[href^="/C"], #menu a[href^="/T"]').live('click',function(e){});


回答2:

Multiple jquery selectors can be separated by commas:

$('#menu a[href^="/C"], #menu a[href^="/T"]').live('click',function(e){});