[英]Comparing a javascript variable with a part of a href
I need to compare a var foo = 'whatever'
with THIS: 我需要将
var foo = 'whatever'
与THIS进行比较:
<ul id="nav_main"><li><a href="index.php?pag=THIS">link</a></li></ul>
And then, put class="current"
inside the matched <li>
. 然后,将
class="current"
放入匹配的<li>
。
var foo = 'whatever';
$("#nav_main li a").filter(function(index){
return foo === this.href.match(/pag=(.*)/)[1];
}).parent().addClass("current");
Would that work? 那行得通吗?
Demo: http://jsfiddle.net/rREry/1 演示: http : //jsfiddle.net/rREry/1
var foo = 'THIS';
$('a[href*="'+foo+'"]', '#nav_main').closest('li').addClass('current');
This searches for <a>
tags that have an href
attribute containing the string, and adds the class to their containing <li>
. 这将搜索具有包含字符串的
href
属性的<a>
标签,并将该类添加到包含<li>
标签中。
$('#nav_main li').each(function() { // loop through each <li>
if ($(this).find('a').attr('href') == foo) { // check against your var (not sure what exactly you wanted to check)
$(this).addClass('current'); // add the "current" class if it matches
}
});
if (foo == "<ul id="nav_main"><li><a href="index.php?pag=THIS">link</a></li></ul>"){
foo.addClass("current")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.