我有以下HTML:
<div class= "field">
<fieldset>
<div style="float: right">
<button> ... </button>
</div>
<div class="some_class">
Some Text
</div>
</fieldset>
</div>
如何使用Jquery检查“Some Text”是否存在?
答案 0 :(得分:0)
只需选择一个DOM元素,然后使用.text()方法。 类似的东西:
var x = $("#DomeElement").text();
if(x=="Some String"){
return true;
}
return false;
答案 1 :(得分:0)
尝试这样的正则表达式测试:
/Some Text/.test( $(this).text() )
$(function() {
$('.field').on('click', function() {
if( /Some Text/.test( $(this).text() ) ) {
alert('"Some Text" was found in clicked field');
} else {
alert('"Some Text" was not found in clicked field');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class= "field">
<fieldset>
<div style="float: right">
<button> ... </button>
</div>
<div class="some_class">
Some Text
</div>
</fieldset>
</div>
<div class= "field">
<fieldset>
<div style="float: right">
<button> ... </button>
</div>
<div class="some_class">
Other Text 1
</div>
</fieldset>
</div>
<div class= "field">
<fieldset>
<div style="float: right">
<button> ... </button>
</div>
<div class="some_class">
Other Text 2
</div>
</fieldset>
</div>
答案 2 :(得分:0)
您是在检查div中是否有任何文本或div中的特定字符串?
if ($.trim($(".some_class").text() == "Specific Text")) {
//do something if specific text is found
}
if ($.trim($(".some_class").text()).length > 0) {
//do something if any text is found
}
如果div中的文本与您要查找的特定字符串匹配,第一个将执行某些操作,如果div中有任何文本,则第二个将执行某些操作。