jQuery,选择包含指定字符串而不包含其他字符串的元素

jQuery, selecting element that contains a specified string while not containing other

本文关键字:字符串 包含 其他 元素 选择 包含指 jQuery      更新时间:2023-09-26

我使用:contains()在文本内容的大页面中查找字符串,并且所有重复块将包含这些块的唯一字符串,例如学生ID。我想把:contains():not(:contains())结合起来

逻辑如下:如果字符串包含1234,但不存在单词"absent",则追加奖励语句

我的目标是将这两个选择器组合起来,产生包含但不包含的效果。

您可以在这里使用filter:

$(selector).contains("1234").filter(function () {
  return $(this).not(":contains("absent")");
});

或者用简单的方式:

$('selector:contains(1234):not(:contains(absent))')