简体   繁体   English

选择内容以'+'开头的所有<a>元素

[英]Selecting all the <a> elements where content starts with '+'

i want to select all the elements where its content starts with + with jquery, how can i do that? 我想用jquery选择其内容以+开头的所有元素,我该怎么做?

example: 例:

<a href="/" >+ Add</a>

You can use filter() from jQuery: 你可以使用jQuery中的filter()

$("a").filter(function() {
    return $.trim(this.innerHTML).indexOf("+") === 0;
}). ...

DEMO: http://jsfiddle.net/4rgNw/ 演示: http //jsfiddle.net/4rgNw/

Here is an alternate, more CSS-driven approach, you can even let your CSS handle adding the plus symbol. 这是一种替代的,更多CSS驱动的方法,你甚至可以让你的CSS处理添加加号。 Checking a class will scale far better than checking the innerHTML of an element, this only really matters if you have a significant amount of + links though. 检查一个类将比检查一个元素的innerHTML要好得多,这只有在你有大量的+链接时才真正重要。

jsFiddle 的jsfiddle

HTML HTML

<a href="/" class="add">Add</a>
<a href="/" class="add">Add</a>

CSS CSS

.add:before { content:"+ "; }

jQuery jQuery的

var addLinks = $('.add');

// or pure JS (faster if you only desire the DOM objects)
addLinks = document.getElementsByTagName('add');

In CSS3 there's a :contains() selector that also might be of use. 在CSS3中有一个:contains()选择器也可能有用。

$('a:contains("+ ")').....

As an example.. Not exactly what you are looking for, yet might turn out to be useful. 作为一个例子..不完全是你想要的,但可能会变得有用。

var links = document.links, l = links.length, i, plus = [];
for (i = 0; i < l;i++){
    if(/\+\w/(links[i].textContent))plus.push(links[i]);
};

Edit: 编辑:

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]);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM