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/