使用JQuery添加CSS类 [英] Adding CSS Class using JQuery

查看:100
本文介绍了使用JQuery添加CSS类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我已经包括jquery代码,我使用尝试添加一个css类到边栏中的链接等于活动的URL,但它不工作,在某一点上它。
预先感谢您提供任何帮助。

Below I have included the jquery code I am using to try to add a css class to the link in the side column that equals the active url, but it's not working, and at some point it did. Thanks in advance for any help you can provide.

链接: http://www.liquidcomma.com/portfolio/project/TSF_Robot_Ad/1/

<script type="text/javascript">
$(document).ready(function(){
      $("ul.right_submenu > li > a").each(function() {
            if ($(this).attr("href") == location.href)
            {
                  $(this).addClass("CurrentProject");
            });
};
</script>


推荐答案

缺少大括号和括号可以做得更简单:

Well, besides that code missing braces and parens it can be done much simpler:

$(function(){
    $("a[href^='" + location.href + "']").addClass("CurrentProject");
});

这篇关于使用JQuery添加CSS类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆