Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jquery";包括「;多值_Javascript_Jquery_Contains - Fatal编程技术网

Javascript Jquery";包括「;多值

Javascript Jquery";包括「;多值,javascript,jquery,contains,Javascript,Jquery,Contains,我在文档中查找所有div,其中包含一个文本:“thetext”,我正在更改此文本: $("div:contains('thetext'):not(:has(*))").each(function () { $(this).text($(this).text() + " anotherTextAddedBefore"); }) 是否可以将包含多个值的内容放入? 我想找到div,其中包含:“thetext”,但也包含另一个值,例如:“thetext1”、“thetext2”等 我想在一个

我在文档中查找所有div,其中包含一个文本:“thetext”,我正在更改此文本:

$("div:contains('thetext'):not(:has(*))").each(function () {

  $(this).text($(this).text() + " anotherTextAddedBefore");

 })
是否可以将包含多个值的内容放入? 我想找到div,其中包含:“thetext”,但也包含另一个值,例如:“thetext1”、“thetext2”等

我想在一个程序中完成,而不是在更多的程序中完成:我不想使用像我想找到的文本那样多的程序


谢谢

您可以使用多重选择器作为条件或类似条件

$("div:not(:has(*))").filter(":contains('thetext'), :contains('thetext2')").each(..)

这样的选择器提供了一个或条件-

$("div:contains('thetext'), div:contains('thetext1'), div:contains('thetext2')")

这样的选择器提供了一个AND条件-

$("div:contains('thetext'):contains('thetext1'):contains('thetext2')")

演示-

您可以创建一个数组并循环它:

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");
    });
}
var containsVals=[“text1”、“text2”];
对于(var i=0;i您可以有一个数组

var array = ['John', 'Martin'];


$(array).each(function () {

    $("div:contains(" + this + ")").css("text-decoration", "underline");

});

只需在上述答案中再添加一点,如果要选择没有特定值的元素,则可以使用

$("div:not(:contains('thetext'), :contains('thetext1'),:contains('thetext2'))")

可以作为
条件
$(“div:contains('thetext')、div:contains('thetext1')、div:contains('thetext2'))工作
$(“div:contains('thetext'):contains('thetext1')):contains('thetext2'))
这不是一个条件@A.Wolff?@JayBlanchard Ya但这就是我理解问题的方式,我可能是错的
我想找到div,其中包含:'thetext',但还有另一个值,例如:'thetext1','thetext2',等等。
啊,重读时我明白你在说什么了@A.WolffThanks下划线!它很短,很简单,很简单很好用!