简体   繁体   English

用文本在div中找到div

[英]Find div inside div with text

I have not paste all the div content here as it is too long 我没有粘贴所有div内容,因为它太长了

<div id="calendar_month">
    <div>
        <div></div>
        <div>DEMO</div>
    </div>
</div>

I have tried this 我试过这个

 $("#calendar_month").children().each(function () {
     if ($(this).text() == "DEMO") {
         $(this).text("");
     }
 });

You can use :contains() 你可以使用:contains()

$( "#calendar_month div:contains('DEMO')" )

Or after you edit your OP: 或者在您编辑OP之后:

 $( "#Calendar > div > div:contains('DEMO')" ).text(""); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <div id="Calendar"> <div> <div>test</div> <div>DEMO</div> </div> </div> 

Or after @BhushanKawadkar comment you can use .filter() : 或者在@BhushanKawadkar评论之后你可以使用.filter()

 $( "#Calendar > div > div" ) .filter(function( index ) { return $(this).text() == "DEMO"; }).text(""); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="Calendar"> <div> <div>test</div> <div>DEMO</div> </div> </div> 

Your div that contains text DEMO is not a direct children of div with id calendar_month . div包含文本DEMO不是直接孩子div id为calendar_month In your HTML children() will return the first div only. 在您的HTML中, children()将仅返回第一个div。

Use find() 使用find()

Try: 尝试:

$("#calendar_month").find('div').each(function () {
     if ($(this).text() == "DEMO") {
         $(this).text("");
     }
 });

DEMO DEMO

Or another way(not recommended but just posting for the logic) to this particular markup is to use 或者使用另一种方式(不推荐但只是发布逻辑)到这个特定的标记

$("#calendar_month").children().children().each(function () {
     if ($(this).text() == "DEMO") {
         $(this).text("");
     }
 });

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

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