如果有包含单一白色空间特征的内容,浏览器可能采取不同的做法。 例如,Safari在保留地时,根本就拆除了它。
jQuery s :contains
filter may not be very useful as it does a search anywhere in the element s text content, but it looks like you want to do an exact search. The problem with :contains
is that when searching for an element with just a comma, it will match all these:
<span>,</span>
<span>hello, world</span>
<span> , </span>
第二个要素和第三个要素将既符合ma,也符合空间。
Here s a custom jQuery filter that is similar to :contains
, but does an exact text match instead.
jQuery.expr[ : ].hasText = function(element, index, meta) {
var textToSearch = meta[3];
return $(element).text() == textToSearch;
};
Use it as :hasTexT(..)
. Here s an example of how to select elements by their text content and change their CSS class.
$( :hasText(,) ).addClass( comma );
$( :hasText( ) ).addClass( space );
http://jsfiddle.net/yAQMe/1/“rel=“nofollow”>example 。 Try it in >。 其他浏览器可以吃上单一白色空间。