为什么在 :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/