当只有一个h1标签时,我有以下小提琴,但我需要它来处理所有h1标签吗?
http://jsfiddle.net/bloodygeese/un06y3x6/
如果我删除了2个h1标签,那么它有效,但是在那里有3个标签,我应该看到应用于其中2个的样式。因为2个h1标签与.duration
匹配<h1>September 14</h1>
<h1>October 10</h1>
<h1>Noveber 12</h1>
<div class="tribe-events-category-ewp">
<div class="duration">Event for September 2014</div>
<div class="tribe-events-category-ewp">
<div class="duration">September 14</div>
<div class="tribe-events-category-ewp">
<div class="duration">October 10</div>
</div>
$(function () {
var h1 = $('h1').text();
$('.duration').filter(function () {
return $(this).text() == h1;
}).css('background-color','#ffff00');
});
答案 0 :(得分:0)
您未正确使用.filter()
,请尝试以下代码:
$('.duration').each(function () {
var myhtml = $(this).text();
var ele = $(this);
$('h1').each(function(){
myhtml == $(this).text() ? $(ele).css('background-color','#ffff00') : "" //or in else case instead of passing empty string you can pass null also.
});
});