Jquery .show()实现与css类



我是Javascript新手,所以这可能很简单,但请指导我。以下代码来自jquery的文档http://api.jquery.com/show/

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
   </head>
    <body>
     <button>Show it</button>
      <p style="display: none">Hello  2</p>
     <script>
     $( "button" ).click(function() {
      $( "p" ).show( "slow" );
     });
    </script>
  </body>
 </html>

我的问题是,如果我想在点击时显示整个内容区域,为什么我不能将"p"更改为类。一个很好的例子可以在www.shopify.com页眉下面看到。

我在想这样的事情

<div class="first-reveal">
 <p style="display:none;">Lorem Ipsum Lorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem Ipsum</p>
</div>
<script>
 $( "button" ).click(function() {
  $( "first-reveal" ).show( "fast" );
 });
</script>

如果你想选择一个类你必须像

 $( "button" ).click(function() {
     $( ".first-reveal" ).show( "fast" );
 });

类选择器为., id选择器为#

$(".myClass")
$("#myId")

与CSS相同:)

first-reveal是一个类,因此,使用jQuery,我们需要在选择器的开头添加一个句号,如下所示:

$( ".first-reveal" ).show( "fast" );

但是,如果它是一个ID,而不是一个类,我们将执行以下操作:

$( "#first-reveal" ).show( "fast" );

最新更新