[英]How to search a text in jquery?
i have a question that if i want to search a perticular text in any page using jquery.我有一个问题,如果我想使用 jquery 在任何页面中搜索特定文本。 what i don't want is that
我不想要的是
<p id="1">iii</p>
<p id="2">i</p>
<p id="3">ibiib</p>
<p id="4">bb</p>
<p id="5">tina</p>
<script type="text/javascript">
console.log($("div:contains('i')"));
</script>
this above code gives me all paragraphs except id=4;上面的代码给了我除 id=4 之外的所有段落; but what i want is only paragraph with id=2 to be selected.
但我想要的只是要选择 id=2 的段落。 if there is a way to do that in javascript or any other library reply me...
如果在 javascript 或任何其他库中有办法做到这一点,请回复我...
$('p').filter(function() {
return $(this).text() === 'i';
});
$("p").each(function(a,b){
if ($(this).html() == 'i'){
console.log(a);
}
});
or或者
$("p").filter(function(){
return $(this).html() == 'i';
});
You can loop through the paragraphs and check if their content is the same as your desired content.您可以循环浏览段落并检查其内容是否与您想要的内容相同。 I used a jQuery loop, you can use javascript if you want.
我使用了 jQuery 循环,如果需要,可以使用 javascript。 JSFIDDLE
JSFIDDLE
$('p').each(function() {
if(this.innerHTML == 'i') doWhateverYouWant();
});
I would use a simple plugin for this:我会为此使用一个简单的插件:
$.fn.exactMatch = function(str) {
return this.filter(function() {
return $(this).text() === str;
});
};
$("p").exactMatch("i").css("border", "1px solid red");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.