gpt4 book ai didi

javascript - 如果元素的后代包含特定单词,则执行代码的 Jquery

转载 作者:行者123 更新时间:2023-11-28 05:25:45 24 4
gpt4 key购买 nike

HTML:

<div id="firstpanel">
<table class="table table-bordered">
<tr>
<td class="alert alert-error">
<span class="errorMsg">Required:</span>
blah blah
</td>
</tr>
</table>
</div>
<div id="second panel">
some text
</div>

如果 firstpanel div 包含单词“Required”,我想执行一些代码

以下代码对我不起作用:

if($("#firstpanel").contains("Required")){

//some code


}

最佳答案

您需要使用 :contains() 选择器

if($('#firstpanel:contains("Required")').length){

//some code

}

:contains() : The matching text can appear directly within the selected element, in any of that element's descendants, or a combination thereof. As with attribute value selectors, text inside the parentheses of :contains() can be written as a bare word or surrounded by quotation marks. The text must have matching case to be selected. ( Taken from https://api.jquery.com/contains-selector/ )

关于javascript - 如果元素的后代包含特定单词,则执行代码的 Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32748027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com