简体   繁体   English

如何使用jquery删除元素类没有img标签?

[英]How to remove class of element has not img tag using jquery?

I want to remove the class description when no <img> tag is found in all the divs with .box1 : 我想在所有带有.box1的div中找不到<img>标签时删除类描述:

<ul class="radio accomodation-radio">
   <li class="homeing-outer0 homeing-outer clearfix active" style="display: block;">
       <div class="box1 clearfix">
            <div class="description">
                 <p>Preis pro Person, nur Mehrbettzimmerbelegung. Sie teilen sich Hotelzimmer im Großraum Reykjavik und 2-4-Bett-Zimmer mit bezogenen Betten während der Tour.</p>
            </div>
       </div>
</li>....

I tried this, but that is not working: 我尝试过这个,但这不起作用:

$(".accomodation-radio").find(".box1 img").each( function(){      
    if ($(this).length === 0){ 
        $(this).find("div").removeClass(".description"); 
    }
});

You can use combination of :not() and :has() selector 您可以使用:not():has()选择器的组合

$(".accomodation-radio .box1:not(:has(img)) div").removeClass("description")

 $(".accomodation-radio .box1:not(:has(img)) div").removeClass("description") 
 .box1 .description{color:green} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="radio accomodation-radio"> <li class="homeing-outer0 homeing-outer clearfix active" style="display: block;"> <div class="box1 clearfix"> <div class="description"> <p>Preis pro Person, nur Mehrbettzimmerbelegung. Sie teilen sich Hotelzimmer im Großraum Reykjavik und 2-4-Bett-Zimmer mit bezogenen Betten während der Tour.</p> </div> </div> </li> 

Your .each() is trying to run on elemnts that don't exist - you need to pass the .find inside the loop like this: 你的.each()试图在不存在的元素上运行 - 你需要像这样在循环内传递.find

$(".accomodation-radio").each(function() {
    if ($(this).find(".box1 img").length === 0)
    { 
        $(this).find("div").removeClass("description"); 
    }
});

Try this : 试试这个 :

$(document).ready(function(){

    $(".accomodation-radio .box1").each(function(){

        if($(this).find("img").length === 0)

            $(this).find(".description").removeClass("description");

    })

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM