admin管理员组

文章数量:1305361

Is there a way to add CSS styling that works only when a specific URL is loaded. I have a URL that has /service/ at the end. Only for this I want some CSS styling to work on a specific Div (myDiv). Is there a way? I cannot add an ID to the body tag of that service page. The page is backoffice generated.

Is there a way to add CSS styling that works only when a specific URL is loaded. I have a URL that has /service/ at the end. Only for this I want some CSS styling to work on a specific Div (myDiv). Is there a way? I cannot add an ID to the body tag of that service page. The page is backoffice generated.

Share Improve this question asked Feb 21, 2017 at 12:18 EddyEddy 5662 gold badges10 silver badges29 bronze badges 2
  • 1 The page is backoffice generated. what does that means? – Vilas Kumkar Commented Feb 21, 2017 at 12:20
  • if(this.href.substr(this.href.lastIndexOf('/') + 1) == 'service') {//your css styling through js} – Rahul Commented Feb 21, 2017 at 12:22
Add a ment  | 

3 Answers 3

Reset to default 3

url.match("/service/$") will match if the url endswith /service/ because $ means endWith in a .match()

Update: To get the url use: window.location.href.match("/service/$")

var url = "www.stackoverflow./questions/service/"
if (url.match("/service/$")) {
  $('#url').css("background-color", "yellow")
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="url"> url </div>

You can use the following:

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

Try with CSS only

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

本文标签: javascriptAdd css style elements only for specific URLStack Overflow