javascript Jquery“包含”多个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23248809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery "contains" multiple values
提问by Za7pi
I am finding all the divs in a document which contains inside a text: 'thetext' and i am changing this text:
我在包含文本内部的文档中找到所有 div:'thetext',我正在更改此文本:
$("div:contains('thetext'):not(:has(*))").each(function () {
$(this).text($(this).text() + " anotherTextAddedBefore");
})
Is it possible to put inside contains multiple values? I would like to find the divs whic contains: 'thetext', but also another values, for example: 'thetext1', 'thetext2',etc
是否可以放入包含多个值?我想找到包含以下内容的 div:“thetext”,但还有另一个值,例如:“thetext1”、“thetext2”等
I`d want to make it in one procedure and not in more: dont want to use as many procedures as texts i′d like to find.
我想在一个程序中而不是在更多程序中完成它:不想使用与我想找到的文本一样多的程序。
Thanks!
谢谢!
回答by Arun P Johny
You can use the multiple selector as a or condition like
您可以将多重选择器用作或条件,例如
$("div:not(:has(*))").filter(":contains('thetext'), :contains('thetext2')").each(..)
回答by Jay Blanchard
A selector like this provides an OR condition -
像这样的选择器提供了一个 OR 条件 -
$("div:contains('thetext'), div:contains('thetext1'), div:contains('thetext2')")
A selector like this provides an AND condition -
像这样的选择器提供了一个 AND 条件 -
$("div:contains('thetext'):contains('thetext1'):contains('thetext2')")
回答by underscore
You can have an array
你可以有一个数组
var array = ['John', 'Martin'];
$(array).each(function () {
$("div:contains(" + this + ")").css("text-decoration", "underline");
});
回答by rakesh
Just adding one more point to above answers, if you want to select elements that dont have particular values then you can use
只需在上述答案中再添加一点,如果您想选择没有特定值的元素,则可以使用
$("div:not(:contains('thetext'), :contains('thetext1'),:contains('thetext2'))")
works as and
condition
作为and
条件工作
回答by Amir Popovich
You can create an array and loop it:
您可以创建一个数组并循环它:
var containsVals = ["text1","text2"];
for(var i=0;i<containsVals.length;i++){
$("div:contains("+ containsVals[i] +"):not(:has(*))").each(function () {
$(this).text($(this).text() + " anotherTextAddedBefore");
});
}