jquery :contains selector using variable

标签 jquery contains

为什么在 :contains() 中使用变量不起作用?有什么办法让它发挥作用吗? filter('contains()') 也不起作用。

<h2>Test</h2>
<div>Test</div>

var model = $('h2').text();
//variable doesn't work
$('div:contains(model)').css('background','yellow');
//obviously, string works
$('div:contains("Test")').css('background','yellow');

最佳答案

选择器不使用 Javascript 变量。您必须将变量的值放入选择器字符串中:

$('div:contains("' + $modele + '")')

关于jquery :contains selector using variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15532746/

相关文章:

javascript - 如何使用 ES6 模块导入 jQuery Masonry?

javascript - 如何总结输入字段中输入的特定值?

php - 字符串包含数组中的任何项目(不区分大小写)

python - Pandas 和应用函数来匹配一个字符串

javascript - 如何在 jQuery 中重新编写 innerHTML

javascript - 阻止 'mouseenter'回调函数直到 'highlight'效果完成

javascript - 如何滚动到具有特定 div 类的第一个可见元素?

swift - 增加 Sprite ContainsPoint 大小

用于列表比较的 Python IN 运算符

c# - string.Replace ("a", "b") 是否自动检查 "a"是否存在?