【问题标题】:Can you select an element with two classes in jQuery你能在jQuery中选择一个有两个类的元素吗
【发布时间】:2010-05-23 02:15:51
【问题描述】:

我有一个元素有两个类,但似乎无法用 jQuery 选择它。 可能吗。 代码如下:

<html>
<head runat="server">
    <script type="text/javascript" src="abc/scripts/jquery-1.4.2.min.js"></script>

    <script type="text/javascript">

      $(document).ready(function() {
        alert($(".x.y").html()); //shows null, I want it to show "456"
      });

    </script>

</head>

<body>

<div class="x" class"y">456</div>

</body>
</html>

【问题讨论】:

    标签: jquery class selector


    【解决方案1】:

    据我所知,拥有两个 class 属性不是有效的 SGML(因此是 HTML)。试试这个:

    &lt;div class="x y"&gt;456&lt;/div&gt;

    【讨论】:

      【解决方案2】:

      您应该能够像这样定位双重课程:

      $(document).ready(function() {
          alert($(".x.y").html()); //shows null, I want it to show "456"
      });
      

      像这样的html:

      <div class="x y">456</div>
      

      【讨论】:

        【解决方案3】:

        这个

        <div class="x" class"y">456</div>
        

        不正确,改成

        <div class="x y">456</div>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-12-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多