Passing two elements to a function with Jquery

I have the following jquery function for a click event on a class:

JS

$('a[rel=blue]').click(function(){
    $('.ffHide').css("display", "none");
});

Now this works great, except I want to pass two classes to the function, how do I do that?

What I want to do:

$('a[rel=blue]', 'a[rel=black]').click(function(){
    $('.ffHide').css("display", "none");
});

but this does not work...


$('a[rel=blue], a[rel=black]').click(function(){
    $('.ffHide').css("display", "none");
});

只是一个旁注 - 你最好使用.on()来附加事件监听器,比如:

$('a[rel=blue], a[rel=black]').on("click", function() {
    $('.ffHide').css("display", "none");
});

'a[rel=blue]', 'a[rel=black]' should be 'a[rel=blue], a[rel=black]' .

$('a[rel=blue], a[rel=black]').click(function(){
   $('.ffHide').css("display", "none");
});

Multiple selectors will be separated by comma within a single quote.

Note

What you've tried is the $(target, context) format, which find the target element within the context .


$('a[rel=blue], a[rel=black]').click(function(){
  $('.ffHide').css("display", "none");
});

很近...

链接地址: http://www.djcxy.com/p/83452.html

上一篇: 汉堡菜单点击问题

下一篇: 将两个元素传递给Jquery的一个函数