admin管理员组

文章数量:1414908

if i have :

<div class="carBig"></div>

and

<div class="car"></div>

and $(".car").size();

i get 2 items ..

if i have :

<div class="carBig"></div>

and

<div class="car"></div>

and $(".car").size();

i get 2 items ..

Share Improve this question edited Nov 26, 2008 at 13:03 bobince 537k110 gold badges672 silver badges844 bronze badges asked Nov 26, 2008 at 13:01 Moran HelmanMoran Helman 18.6k4 gold badges25 silver badges26 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

What version of jquery are you using?

Using this code:

<html><head><title>Testing</title>
<script type="text/javascript" src="/js/jquery/jquery-1.2.6.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
    $(".car").each(function() {
        $("#carResults").append($(".car").size());
        $("#carResults").append($(this).text());
    });
});
</script>
</head><body>
<div class="carBig">Big Car</div>
<div class="car">Regular Car</div>
<div id="carResults"></div>
</body></html>

My output document looked like this:

Big Car
Regular Car
1Regular Car

Mine only found 1 element, the one with the class of "car"...

I think you may have something funky somewhere that's throwing it off. If I run this very simple example, it works just as expected.

<html>
<head>
</head>
<script type="text/javascript" src="jquery-1.2.6.pack.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $(".car").hide();
    });
</script>
<body>
    <div id=container>
        <div class="carBig">Car Big</div>
        <div class="car">Car</div>
    </div>
</body>
</html>

You could try posting the rest of your html to see if we can figure it out.

本文标签: javascriptWhy jquery class selector select items that has part of the classnameStack Overflow