开发者

Can I "stack up" selectors using jQuery?

开发者 https://www.devze.com 2023-01-20 22:53 出处:网络
I know \"selectors\" is the wrong term, but I can\'t remember it :P I have this script: $(function() {

I know "selectors" is the wrong term, but I can't remember it :P

I have this script:

$(function() {
   //checkbox
   $("#checkbox2, .akslabel").click(function(){
    $("#scrollwrap").toggleClass('highlight');
   });
});
开发者_高级运维

Is it ok to stack up the selectors like that? Or should I re-write it for each one?

Thanks!


You can use as many selectors as you want combined with a , (the multiple selector) and bind the same anonymous function to them all, which is what your example does.

There's nothing wrong with this, it's cleaner/more concise code.

So can you "stack" selectors? Yes, think of them as waffles, very delicious waffles, with bacon.


Yes you can use a comma-separator for binding events to multiple elements with different classnames/id's/etcetera.

If it isn't working, you have class "akslabel", might want to change that to "asklabel"...?


It's ok, it's even better then splitting. Why duplicate code?

0

精彩评论

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