简体   繁体   English

jquery :如果 url 包含字符串 x 更改特定标签的背景颜色

[英]jquery : If url contains string x change backgroud color of specific tag

I want to change color backgroud of tag in my code depending on URL.我想根据 URL 在我的代码中更改标签的颜色背景。 If url contains Suisse , it changes the background color to green for the tag #switch-univers li a.bilgroup如果 url 包含Suisse ,它会将标签#switch-univers li a.bilgroup的背景颜色更改为绿色

JS JS

if (window.location.href.indexOf("Suisse") > -1) {
  $('#switch-univers li a.bilgroup').css({ backgroundColor, 'green' });
} else if window.location.href.indexOf("Sales") > -1) {
  $('#switch-univers li a.sales').css({ backgroundColor, 'blue' });
} else if (window.location.href.indexOf("My%20desktop") > -1) {
  $('#switch-univers li a.lifebil').css({ backgroundColor, 'pink' });
}

And My HTML is :我的 HTML 是:

<body>
  <Div id= « switch-univers » >
    <li><a class=“bilgroup“>Suisse</a></li>
    <li><a class=“sales“>Sales</a></li>
    <li><a class=“lifebil“>My desktop</a>
    </li>
  </div>
</body>

Nothing happen... Any Suggestions什么都没有发生...任何建议

Try this.尝试这个。

<div id="switch-univers">
    <li><a class="bilgroup">Suisse</a></li>
    <li><a class="sales">Sales</a></li>
    <li><a class="lifebil">My desktop</a>
    </li>
</div>

JQuery:查询:

$(document).ready(function(){
 if (window.location.href.indexOf("Suisse") > -1) {
  $('#switch-univers li a.bilgroup').css('background-color', 'green');
 } else if window.location.href.indexOf("Sales") > -1) {
  $('#switch-univers li a.sales').css('background-color', 'blue');
 } else if (window.location.href.indexOf("My%20desktop") > -1) {
  $('#switch-univers li a.lifebil').css('background-color', 'pink');
 }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM