开发者

How use jQuery's contains() to find special char ( and )

contains() is used to find a string / text.

But my text includes ( and ). Like bank of america(wes开发者_如何学JAVAt)

And it does not work.


I think you misunderstood meaning of .contains()

jQuery.contains(document.documentElement, document.body); // true
jQuery.contains(document.body, document.documentElement); // false

Description: Check to see if a DOM element is within another DOM element.

http://api.jquery.com/jQuery.contains/

You probably mean :contains

http://sandbox.phpcode.eu/g/462b6/5

<div id="element">bank of america (test) here</div>

<script>
$(function(){
   $("#element:contains('bank of america (test)')").css('background-color', 'yellow');
});
</script>

works


.Contains() finds DOM elements, not text. If you want to find if text is within a string or not, the simplest way is to use .indexOf().

e.g.

'this is a test'.indexOf('is a');

returns 5, the position of the first instance of the text in the string. If the text was not matched it would return -1. Therefore to check if the text is contained within the string you just do:

if(theString.indexOf(searchText) > -1) {
  ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜