Javascript Jquery-拆分Url并将类添加到正文

Javascript Jquery-拆分Url并将类添加到正文,javascript,jquery,url,Javascript,Jquery,Url,我需要得到url,将其拆分为单词,然后将这些类添加到正文中。 我的url示例(有时包括?和#): 我希望所有的单词在正文中都有等级: 查询类型或过滤1filter\u coramarelofilter\u estilo过滤塔曼霍gg 我试过: $(window).on('hashchange', function(e){ $('body'). }); 我把这件衣服放在一堆里,但它没有把字分开 function updateBodyClasses() { var clas

我需要得到url,将其拆分为单词,然后将这些类添加到正文中。 我的url示例(有时包括
#
):

我希望所有的单词在正文中都有等级:
查询类型
过滤
1
filter\u cor
amarelo
filter\u estilo
过滤塔曼霍
gg

我试过:

$(window).on('hashchange', function(e){
       $('body'). 
});
我把这件衣服放在一堆里,但它没有把字分开

function updateBodyClasses() {

  var classNames = [];

  // Use `.split()` to separate each key/value pair in the query by `&`
  window.location.search.split('&')
    // and loop over the results
    .forEach(function(c) {

      // Now split each pair by '='
      var pair = c.split['='];

      // Add the value to the left of the '=' 
      if (pair.length > 0) {
        classNames.push(pair[0]);

        // if there are values on the right of the '='...
        if (pair.length > 1) {

          // ... split them by ',' and loop through them
          pair[1].split(',').forEach(function(t) {
            classNames.push(t);
          });      
        }
      }
    });

    // Now append those classNames to the body
    $('body').addClass(classNames.join(' '));
}

// If your ajax plugin modifies the url via HTML5 history api...
$(window).on('popstate', updateBodyClasses);


// Update classes on page load
$(window).on('load', updateBodyClasses);

// If the ajax plugin modifies the url using window.location.replace()
// you'll need to check on an interval whether things have changed
// and update accordingly. the following example does this every 1000 milliseconds (or every second)

setInterval(updateBodyClasses, 1000);

你走得太久了

首先删除url,直到包含它。
使用
&

拆分 对数组中的每个函数使用一个,并将每个函数添加到body的类列表中

代码
var url=window.location.href->这将把当前url存储到
url

url=url.replace(/[^?#]*[?#]/,''->这将删除
?|#
之前的零件,包括它
url=url.split(/[&=]/)->这将在
&|=
上拆分它,其结果是一个数组
url.forEach(x=>document.body.classList.add(x))->将所有条目添加到正文的类列表中

var url=window.location.href;
url=url.replace(/[^?#]*[?#]/,“”);
url=url.split(/[&=]/);

forEach(x=>document.body.classList.add(x))你走得太长了

首先删除url,直到包含它。
使用
&

拆分 对数组中的每个函数使用一个,并将每个函数添加到body的类列表中

代码
var url=window.location.href->这将把当前url存储到
url

url=url.replace(/[^?#]*[?#]/,''->这将删除
?|#
之前的零件,包括它
url=url.split(/[&=]/)->这将在
&|=
上拆分它,其结果是一个数组
url.forEach(x=>document.body.classList.add(x))->将所有条目添加到正文的类列表中

var url=window.location.href;
url=url.replace(/[^?#]*[?#]/,“”);
url=url.split(/[&=]/);

forEach(x=>document.body.classList.add(x))完成@萨加尔·维多内@萨加尔五世