Add multiple variables on javascript function?

2020-05-04 12:39发布

问题:

How can I pass multiple variables on a jquery function? Like so:

$(".link1",".link2").click(function(event){

}

Also tried:

$(".link1"),$(".link2").click(function(event){

}

回答1:

Multiple selectors need only be separated by a comma in your selector string.

$(".link1, .link2").click(function(event){

}


回答2:

Use

$(".link1, .link2").click(function(event){

or

$(".link1").add(".link2").click(function(event){

The comma separator is defined here : Multiple selector



回答3:

use .on, and pass the extra events as an argument to that method:

$('.link1, link2').on('click',{some:'argument'},function(e)
{
    console.log(e.data);//<-- object literal {some:'arguments'}
});

Read all about it here