【问题标题】:Assigning more than one class for one event为一项活动分配多个班级
【发布时间】:2012-05-17 17:18:03
【问题描述】:

我有一个点击事件,我想分配给更多的班级。原因是我在应用程序的不同位置使用此事件,而您单击的按钮在不同位置具有不同的样式。

我想要的是 $('.tag' '.tag2') 之类的东西,当然行不通。

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

        else {
             // and here
        }
    });

【问题讨论】:

标签: jquery class events


【解决方案1】:

方法#1

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

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

// or, simplifying further
$(".tag1, .tag2").click(doSomething);

方法#2

这也可以:

​$(".tag1, .tag2").click(function(){
   alert("clicked");    
});​

Fiddle

如果有可能重用逻辑,我更喜欢单独的函数(方法 #1)。

另请参阅How can I select an element with multiple classes? 以处理同一项目上的多个类。

【讨论】:

  • 在第一个示例中,您不需要$('.tag2').click(doSomething);,因为很明显,两个/或/更多元素都有一个公共类.tag,但只有特定的clickedTagjsbin.com/ukiguf/2/edit跨度>
  • @RokoC.Buljan - OP 说“我有一个点击事件,我想分配给更多的类。”我正在展示一种方法来做到这一点。我不清楚是否为所有相关元素分配了一个公共类。如果是这样,该方法仍然有效。
  • 没错,不止一个是:.tag.clickedTag ;) 好吧。来自我的 +1(P.S:OP 需要比我们更多的投票。[你曾经需要悬赏一个问题吗?])
【解决方案2】:

您可以像这样使用 jQuery 一次选择多个类:

$('.tag, .tag2').click(function() {
    var $this = $(this);
    if ($this.hasClass('tag')) {
        // do stuff
    } else {
        // do other stuff
    }
});

为 $()​​ 函数提供第二个参数,限定选择器的范围,以便 $('.tag', '.tag2') 在类 tag2 的元素中查找 tag

【讨论】:

    【解决方案3】:
        $('.tag1, .tag2').on('click', function() {
    
          if ($(this).hasClass('clickedTag')){
             // code here
          } else {
             // and here
          }
    
       });
    

    function dothing() {
       if ($(this).hasClass('clickedTag')){
            // code here
        } else {
            // and here
       }
    }
    
    $('.tag1, .tag2').on('click', dothing);
    

     $('[class^=tag]').on('click', dothing);
    

    【讨论】:

      【解决方案4】:

      是这样的:

      $('.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
      }
      

      【讨论】:

      • 组合点击功能,正是我要找的,谢谢!
      • @Systembolaget 你的昵称让我回到过去 5 年:P
      • 我的荣幸 :) 未来不再像过去那样!
      【解决方案5】:

      你试过这个吗:

       function doSomething() {
           if ($(this).hasClass('clickedTag')){
               // code here
           } else {
               // and here
           }
       }
      
       $('.tag1, .tag2').click(doSomething);
      

      【讨论】:

        【解决方案6】:
            $('[class*="tag"]').live('click', function() {
              if ($(this).hasClass('clickedTag')){
                 // code here
              } else {
                 // and here
              }
           });
        

        【讨论】:

          猜你喜欢
          • 2011-10-10
          • 1970-01-01
          • 2013-01-31
          • 2012-05-20
          • 2016-06-06
          • 2023-04-06
          • 1970-01-01
          • 1970-01-01
          • 2021-12-24
          相关资源
          最近更新 更多