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
3 Answers
Reset to default 3url.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
版权声明:本文标题:javascript - Add css style elements only for specific URL - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741730339a2394811.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论