[英]Add a class to the current active link using YUI
I got this function in jQuery: 我在jQuery中获得了以下功能:
jQuery(function() {
jQuery('.primary-nav li').each(function() {
var href = jQuery(this).find('a').attr('href');
if (href === window.location.pathname) {
jQuery(this).addClass('current');
}
});
});
but unfortunately I need to do accomplish the same with the YUI library. 但不幸的是,我需要对YUI库做同样的事情。 Add a class to the a element if the a href is the same as the current active page.
如果a href与当前活动页面相同,则将类添加到a元素中。
Thanks a lot! 非常感谢!
An alternative: 替代:
YUI().use('node', function(){
Y.all('.primary-nav li').each(function(node){
var href = node.getAttribute('href');
node.toggleClass('current', href === window.location.pathname);
});
});
Adds the class if the second parameter is true, otherwise removes it. 如果第二个参数为true,则添加该类,否则将其删除。
An almost direct translation of the JQuery above would be something like: 上面的JQuery的几乎直接翻译是这样的:
YUI().use('node', function(){
Y.all('.primary-nav li').each(function(node){
var href = node.getAttribute('href');
if (href === window.location.pathname){
node.addClass('current');
}
});
});
However, I would imagine you could do something like: 但是,我想您可以执行以下操作:
YUI().use('node', function(){
Y.one('.primary-nav li a[href="' + window.location.pathname + '"]').addClass('current');
});
To achieve the same effect. 达到同样的效果。 (code tested only in my head)
(仅在我的脑海中测试过的代码)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.