[英]Add class, closest div
Almost have 3 hours spend on this. 几乎花了3个小时。 Nothing found. 没有发现。
Here is my code.. 3 separate URL on file. 这是我的代码。文件上有3个单独的URL。 (Updated) (更新)
<div id="menu">
<a href="blue.php">
<div class="list">
Content_here
</div>
</a>
<a href="yallow.php">
<div class="list">
Content_here
</div>
</a>
<a href="black.php">
<div class="list">
Content_here
</div>
</a>
</div>
Add class on current URL 在当前网址上添加课程
<div id="menu">
<a href="#">
<div class="list active">
content_here
</div>
</a>
</div>
Here is: 这是:
$("#menu a").each(function(index) {
if($.trim(this.href) == window.location) {
$(this).closest('div').addClass("active");
}
});
No active
class
on current URL
. 当前URL
上没有active
class
。 ... ...
The closest()
method is using for getting the ancestor element.Since the div is inside the a
use children()
or find()
method. 在closest()
方法是使用用于获取祖先element.Since股利是里面a
使用children()
或find()
方法。
$(this).find('div').addClass("active");
Or select it based on the this
context . 或根据this
上下文选择它 。
$('div', this).addClass("active");
Although for getting the current page url use window.location.href
. 尽管要获取当前页面的url,请使用window.location.href
。
$("#menu a").each(function(index) {
if($.trim(this.href) == window.location.href) {
$('div', this).addClass("active");
}
});
or use condition as window.location.pathname == $(this).attr('href')
或将条件用作window.location.pathname == $(this).attr('href')
try this: 尝试这个:
$("[url="+window.location.origin+"]").addClass("active")
Please note that window.location
is an object and not a string 请注意, window.location
是一个对象,而不是字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.