Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery基于页面URL添加类_Javascript_Jquery_Css_Url - Fatal编程技术网

Javascript jQuery基于页面URL添加类

Javascript jQuery基于页面URL添加类,javascript,jquery,css,url,Javascript,Jquery,Css,Url,我有一个我认为很简单的问题,但我无法让它在我的生活中发挥作用 我所要做的就是向页面添加一些javascript,根据URL向主页容器添加一个类 假设我在root.com上有一个站点和以下html结构(粗略地说): 废话,废话,废话 我想做的是一个脚本,如果页面=(例如)root.com/technology,它会向主div添加一个类。因此div现在看起来像: <div id="main" class="wrapper tech"> 我已经加载了jquery,所以我

我有一个我认为很简单的问题,但我无法让它在我的生活中发挥作用

我所要做的就是向页面添加一些javascript,根据URL向主页容器添加一个类

假设我在root.com上有一个站点和以下html结构(粗略地说):


废话,废话,废话
我想做的是一个脚本,如果页面=(例如)root.com/technology,它会向主div添加一个类。因此div现在看起来像:

     <div id="main" class="wrapper tech">


我已经加载了jquery,所以我想这样做。

使用内置对象来确定完整URL或其某些部分。

是否需要以下帮助?基于另一个答案,只要做

console.log(window.location)

,您可以获得大量与位置对象相关的信息

     if(window.location.href=== "root.com/technology") {
         $("#main").addClass("tech");
     }

您可以使用
window.location
获取当前URL,然后根据该URL进行切换:

$(function() {
  var loc = window.location.href; // returns the full URL
  if(/technology/.test(loc)) {
    $('#main').addClass('tech');
  }
});

这使用正则表达式查看URL是否包含特定短语(特别是:
技术
),如果是,则向
#main
元素添加一个类

switch (window.location.pathname) {
    case '/technology':
        $('#main').addClass('tech');
        break;
    case '/something':
        // code block
        break;
    case '/somestuff':
        $('#main').addClass('some');
        break;
    default: 
        // code block
}
通过这种方式,您可以保持代码干净,并且可以轻松地添加另一个案例


看看
window.location.pathname的意思。

我喜欢这个,因为正如你所说,你可以很容易地添加另一个案例,而不会把事情搞砸。但我不能让它工作…我喜欢这个答案比选择的“最佳”答案更好。Switch语句比写出条件语句干净得多。@MattMartin这可能是因为您需要添加
break在每个案例的结尾处。对我来说就是这样。。Lol显然这是正确的,但它并没有完全回答我的问题。@alex vidal它对我来说很好,但加载时间比正常情况晚了一点。我使用此操作在用户处于确定状态时保持子菜单打开,因此用户可以在1-2秒后查看子类别菜单如何打开。我希望它能立即做到这一点,我如何才能避免这种情况?如何可以修改,以工作,如果你在路线(主页)的网站?谢谢!你能告诉我
.test
做什么吗?
      $(function(){
        var cUrl = window.location.href;
         $(".product-type-swatches__list a").each(function(){
                 if ($(this).attr("href") == cUrl){
                         $(this).addClass("is-selected");
                 }
         });
    });
      $(function(){
        var cUrl = window.location.href;
         $(".product-type-swatches__list a").each(function(){
                 if ($(this).attr("href") == cUrl){
                         $(this).addClass("is-selected");
                 }
         });
    });