开发者

jQuery: If all children have same class

开发者 https://www.devze.com 2023-04-06 21:01 出处:网络
How do I check if all children, or all selectors, have same class? The class is unknown... <script>

How do I check if all children, or all selectors, have same class?

The class is unknown...

<script>
    $(document).ready(function() {
        var symbols = $("div:first-child").attr("class");

        if ($("div").hasClass(symbols).length == 3开发者_JAVA百科) {
            console.log("same");
        };
    });
</script>
<div class="john"></div>
<div class="john"></div>
<div class="john"></div>

This doesn't work... :-/


$("div").not('.john').length

If any of the divs are not class john this will find them, then you check the length and if it's not zero then some exist.

This is a problem:

$("div:first-child").attr("class")

It will return the entire class string, but the div could have more than one class, and all will be returned. But when you check with either my code or hasClass you can only send in one class, not a bunch together.


HTML:

<div class="parent"> 
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
</div>

jQuery:

if ($(".parent").children().length == $(".parent").children(".child").length) {
    alert("wooo all the things have teh same class");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消