如果在LI中存在特定文本並相應地添加一個類,則需要使用jQuery進行檢查。檢查某些文本並添加類
東西沿着這些路線...
if ($('ul li:last-child:contains("this text")').length > 0) {
$(this).addClass("this");
} else if ($('ul li:last-child:contains("that text")').length > 0)
$(this).addClass("that");
}
顯然,上面的代碼不工作,但希望它是接近到什麼,我需要。任何人都知道我該怎麼寫呢?
編輯: 這裏是感謝的是爲我工作,以低於adeneo的回答確切的代碼...
$('.job-result .job-details ul li:last-child').addClass(function() {
return $(this).text().indexOf('Augusta') != -1 ? 'augusta' :
$(this).text().indexOf('Aiken') != -1 ? 'aiken' :
$(this).text().indexOf('Job') != -1 ? 'jobshop' : '';
});
你可以設置提琴手? – Amit
顯示你的html。 –
'$(this)'是指什麼? –