仅为特定URL添加css样式元素

时间:2017-02-21 12:18:26

标签: javascript jquery css

是否有一种方法可以添加仅在加载特定URL时有效的CSS样式。我的网址末尾有 / service / 。只是为了这个我想要一些CSS样式来处理特定的Div(myDiv)。有办法吗?我无法在该服务页面的body标签中添加ID。该页面是后台生成的。

3 个答案:

答案 0 :(得分:2)

如果网址以url.match("/service/$")结尾,

/service/将匹配,因为$表示endWith中的.match()

更新:要使用网址:window.location.href.match("/service/$")



var url = "www.stackoverflow.com/questions/service/"
if (window.location.href.match("/service/$")) {
  $('#url').css("background-color", "yellow")
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="url"> url </div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用以下内容:

if (window.location.href.indexOf('/service/') !== -1) {
  document.getElementById("myDiv").className += ' specialclass';
}

答案 2 :(得分:0)

仅使用CSS

[href$='home']{
    color : red;
}
<a href="something/home">home</a>
<a href="something/home/somethingelse">home</a>

相关问题