【发布时间】:2013-03-10 02:10:12
【问题描述】:
为什么在 :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');
【问题讨论】:
-
重复:在此处查看答案:stackoverflow.com/questions/2191419/…
-
与文本节点无关,JS不解析字符串内的变量(看来你可能习惯了PHP?)你必须使用
$('div:contains('+$modele+')').css('background','yellow');