![](/img/trans.png)
[英]change background color of div if it contains an href that contains a specific string with jquery
[英]jquery : If url contains string x change backgroud color of specific tag
我想根據 URL 在我的代碼中更改標簽的顏色背景。 如果 url 包含Suisse ,它會將標簽#switch-univers li a.bilgroup
的背景顏色更改為綠色
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' });
}
我的 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>
什么都沒有發生...任何建議
嘗試這個。
<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>
查詢:
$(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.