如何根据URL路径添加CSS类? [英] How to add a CSS class depending on URL path?

查看:145
本文介绍了如何根据URL路径添加CSS类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据我所处的路径将CSS类添加到div中,包括如果我在其中包含#号,那么它应该不正确?

How can I add an CSS class to an div depending on what path I am on, including that it should not mather if I had # in it?

<div class="popup">
    <ul>
        <li><a href="#vs">Example 1</a></li>
        <li><a href="#bod">Example 2</a></li>
        <li><a href="#ptf">Example 3</a></li>
    </ul>
</div><!-- popup -->

<!-- how would I add the class addMe with javascript depending on the site path? It has to work with # -->

jsfiddle.net/zatox50/wBxkj/

示例index.html

Example index.html

推荐答案

jQuery(function($){
   switch(window.location.hash){
      case "vs":  $(".popup").addClass("class1"); break;
      case "bod":  $(".popup").addClass("class2"); break;
      case "ptf":  $(".popup").addClass("class3"); break;
   }
});

这篇关于如何根据URL路径添加CSS类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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