Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 为一个事件分配多个类_Jquery_Class_Events - Fatal编程技术网

Jquery 为一个事件分配多个类

Jquery 为一个事件分配多个类,jquery,class,events,Jquery,Class,Events,我有一个单击事件要分配给多个类。原因是我在应用程序的不同位置使用此事件,并且您单击的按钮在不同位置具有不同的样式 我想要的是类似$'.tag'.tag2'的东西,这当然不起作用 $('.tag').click(function (){ if ($(this).hasClass('clickedTag')){ // code here } else { // and here

我有一个单击事件要分配给多个类。原因是我在应用程序的不同位置使用此事件,并且您单击的按钮在不同位置具有不同的样式

我想要的是类似$'.tag'.tag2'的东西,这当然不起作用

    $('.tag').click(function (){
        if ($(this).hasClass('clickedTag')){
            // code here
        }

        else {
             // and here
        }
    });
方法1 方法2 这也将有助于:

​$(".tag1, .tag2").click(function(){
   alert("clicked");    
});​
如果有可能重用逻辑,我更喜欢单独的函数方法1

有关在同一项目上处理多个类的信息,请参见。

如下所示:

$('.tag.clickedTag').click(function (){ 
 // this will catch with two classes
}

$('.tag.clickedTag.otherclass').click(function (){ 
 // this will catch with three classes
}

$('.tag:not(.clickedTag)').click(function (){ 
 // this will catch tag without clickedTag
}
$('.tag, .tag2').click(function() {
    var $this = $(this);
    if ($this.hasClass('tag')) {
        // do stuff
    } else {
        // do other stuff
    }
});

使用jQuery可以一次选择多个类,如下所示:

$('.tag.clickedTag').click(function (){ 
 // this will catch with two classes
}

$('.tag.clickedTag.otherclass').click(function (){ 
 // this will catch with three classes
}

$('.tag:not(.clickedTag)').click(function (){ 
 // this will catch tag without clickedTag
}
$('.tag, .tag2').click(function() {
    var $this = $(this);
    if ($this.hasClass('tag')) {
        // do stuff
    } else {
        // do other stuff
    }
});
为$function提供第二个参数,限定选择器的作用域,以便$'.tag'、'.tag2'在类为tag2的元素中查找标记。

您尝试过以下方法吗:

 function doSomething() {
     if ($(this).hasClass('clickedTag')){
         // code here
     } else {
         // and here
     }
 }

 $('.tag1, .tag2').click(doSomething);


在第一个示例中,您不需要$'.tag2'。单击DoSomething;原因很明显,两个/或多个元素都有一个通用的class.tag,但只有特定的clickedTag@RokoC.Buljan-OP说我有一个click事件,我想分配给多个类。我正在展示一种方法。我不清楚是否有一个公共类分配给所有相关元素。如果是这样的话,这种方法仍然有效。确切地说,不止一种方法是:。tag和.clickedTag;好的+我的建议:OP比我们需要更多的选票。[你曾经需要悬赏一个问题吗?]应该有帮助,有一个好问题!干杯!组合点击功能,正是我想要的,谢谢@你的昵称让我回到了过去5年:我很高兴:未来不再是过去了!
function dothing() {
   if ($(this).hasClass('clickedTag')){
        // code here
    } else {
        // and here
   }
}

$('.tag1, .tag2').on('click', dothing);
 $('[class^=tag]').on('click', dothing);
    $('[class*="tag"]').live('click', function() {
      if ($(this).hasClass('clickedTag')){
         // code here
      } else {
         // and here
      }
   });