我希望使用jQuery选择所有内容以“+”开头的元素,该怎么做?
示例:
示例:
<a href="/" >+ Add</a>
<a href="/" >+ Add</a>
filter()
方法:$("a").filter(function() {
return $.trim(this.innerHTML).indexOf("+") === 0;
}). ...
$.trim()
?它似乎在没有它的情况下也能正常工作:http://jsfiddle.net/chrisJamesC/4rgNw/1/ - Christopher Chiche/^\s*\+/.test( this.innerHTML )
怎么样? - rab<a href="/" class="add">Add</a>
<a href="/" class="add">Add</a>
CSS
.add:before { content:"+ "; }
jQuery
var addLinks = $('.add');
// or pure JS (faster if you only desire the DOM objects)
addLinks = document.getElementsByTagName('add');
var links = document.links, l = links.length, i, plus = [];
for (i = 0; i < l;i++){
if(/\+\w/(links[i].textContent))plus.push(links[i]);
};
编辑:
var links = document.links, l = links.length, i, plus = [];
for (i = 0; i < l;i++){
if(/\+\w/.test(links[i].textContent))plus.push(links[i]);
};
$('a:contains("+ ")').....
举个例子.. 不完全是你要找的,但可能会有用。
:contains
在字符串中搜索存在,不能用于*"以...开头"*的情况。 - VisioN
class="add"
添加到链接中,然后使用$(".add")
来选择它们。我发现这比在元素中匹配文本更清晰。 - Adam Tomat