我在一个动态生成的页面上有一个链接,它的href看起来像<a href="/ListJobs/ByCustom/Job-Category/Keyword-Accounting-Finance/">Accounting/Finance</a>
或<a href="/ListJobs/ByCustom/StationLocation/Keyword-KCCI-TV/">KCCI-TV</a>
,我需要根据链接的Job Category或StationLocation部分更改h1标记。我试过使用
if ($("table a[href='/ListJobs'][href='/ByCustom'][href='/StationLocation']")) {
alert($('table a').attr("href"));
} else {
alert("Category");
$('#mainbody h1').html("Careers by Category");
}
以及包括在内的许多其他变体
$("table a [href|='/ListJobs/ByCustom/Job-Category']
所有的努力都没有达到预期的效果。我应该使用什么来查看href是否包含我需要的字符串部分,以便相应地更改h1标记?
$("table a").each(function() {
if (this.href.indexOf('StationLocation') != -1) {
alert($('table a').attr("href"));
} else {
alert("Category");
$('#mainbody h1').html("Careers by Category");
}
});
使用^="starts with"运算符怎么样?
if ($("table a[href^='/ListJobs'][href^='/ByCustom'][href^='/StationLocation']")) {
alert($('table a').attr("href"));
} else {
alert("Category");
$('#mainbody h1').html("Careers by Category");
}
http://api.jquery.com/attribute-starts-with-selector/