你能在jQuery中选择一个带有两个类的元素吗?

Pet*_*ras 1 jquery class selector

我有一个元素有两个类,但似乎无法用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>
Run Code Online (Sandbox Code Playgroud)

Chr*_*ris 8

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

<div class="x y">456</div>
Run Code Online (Sandbox Code Playgroud)


xan*_*ded 8

您应该能够像这样定位双类:

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

用这样的HTML:

<div class="x y">456</div>
Run Code Online (Sandbox Code Playgroud)