【问题标题】:jQuery id class selctions ".click"jQuery id 类选择器“.click”
【发布时间】:2014-11-03 23:43:45
【问题描述】:
<script>
    $( "#oth" ).click(function() {
        $( "#foli9" ).show( "slow" );
    });

    $( "#fam" ).click(function() {
        $( "#foli9" ).hide( 1000 );
    });
</script>

是否可以在我选择 id "#fam" 的地方选择多个 id。比如“#fam、#test、#another”?

所以点击它会应用到多个id?

【问题讨论】:

  • 我认为使用 class 而不是 ids 是一种更好的方法,而且您应该在询问这个 qustiong google 之前进行一些研究:select different ids jquery first result http://api.jquery.com/multiple-selector/

标签: jquery tags click css-selectors


【解决方案1】:

你试过了吗?

$("#fam, #test, #another").click(function() { ...

应该可以正常工作。请参阅multiple selector 的 jQuery API 文档。

【讨论】:

    【解决方案2】:

    是的,jQuery 使用 css 选择器,因此您可以用逗号分隔选择器列表:

    $( "#fam, #test, #another" ).click(function() {
        // do stuff
    });
    

    【讨论】:

      【解决方案3】:

      是的,可以使用 jQuery 在选择器中使用多个 id。

      您可以像这样使用多个 id:

      $("#fam, #test, #another") 
      

      另一种方法是为他们三个都指定一个类名(相同)并执行:

      $(".your_class_id")
      

      【讨论】:

        【解决方案4】:

        id 的多个选择器工作正常:

         $("#fam, #test, #another").on("click", function() { ... });
        

        但是,必须确保 id 是唯一的。因此,为这三个元素定义一个公共类(比如myClass)并使用

         $(".myClass").on("click", function() { ... });
        

        改为。

        【讨论】:

          猜你喜欢
          • 2013-03-01
          • 1970-01-01
          • 2017-08-16
          • 1970-01-01
          • 2011-06-07
          • 1970-01-01
          • 2010-10-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多