admin管理员组文章数量:1297119
i have a question that if i want to search a perticular text in any page using 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; but what i want is only paragraph with id=2 to be selected. if there is a way to do that in javascript or any other library reply me...
i have a question that if i want to search a perticular text in any page using 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; but what i want is only paragraph with id=2 to be selected. if there is a way to do that in javascript or any other library reply me...
Share Improve this question asked Aug 16, 2011 at 12:12 teacherteacher 1,0153 gold badges15 silver badges28 bronze badges 2- Do you want to for the full content of elements, or for words? – Felix Kling Commented Aug 16, 2011 at 12:16
- 2 Exact duplicate: stackoverflow./questions/7076159/… – Patrick Commented Aug 16, 2011 at 12:16
6 Answers
Reset to default 5Use $.filter.
This will return the div
s, which contains just one i
.
$('div').filter(function () {
return $(this).html() == 'i';
});
$('p').filter(function() {
return $(this).text() === 'i';
});
You can extend jQuery's selector engine to add a new one:
$.expr[':'].texteq = function( elem, i, match, array ) {
return $(elem).text() === match[3];
};
console.log($('p:texteq(i)'));
Example
$("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. 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");
You can try it here.
本文标签: javascriptHow to search a text in jqueryStack Overflow
版权声明:本文标题:javascript - How to search a text in jquery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741641044a2389918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论