例如:
<div class="el">   ;  ;  ;  ;HasNbsp</div>
<div class="el">NoNbsp</div>
我想选择“&amp; nbsp”这是HTML的“.el”,我试过了:
$("div.el").html().contains(" ")
但jquery中不允许这样做。
这甚至可能吗?以及如果是的话。感谢。
注意:我不需要删除“&amp; nbsp”。我真的需要“&amp; nbsp;”
答案 0 :(得分:4)
试试这个:
$(function() {
var regEx = / /gm;
var elements = $("div p").filter(function(index, ele) {
if(regEx.test($(ele).html())) {
return true;
}
});
// elements will contains DOM elements which has
});
<强> DEMO 强>
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="result">
<p> This contains space</p>
<p>hi, this doesn't</p>
</div>
<script type="text/javascript">
$(function() {
var regEx = / /gm;
var elements = $("div p").filter(function(index, ele) {
if(regEx.test($(ele).html())) {
console.log("matched");
return true;
}
});
$(elements).each(function() {
$(this).css({background: "red"});
})
});
</script>
</body>
</html>
&#13;
答案 1 :(得分:3)
答案 2 :(得分:0)
您想要过滤您的查询:
$("div.el").filter(function(){
return $(this).html().indexOf(" ") > -1;
});
这将返回函数评估的元素为&#34; true&#34;,因此只需让您的函数在包含&#34;&amp; nbsp&#34;。
的元素上返回true。