亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

jquery 在不在特定頁面上時應用樣式

jquery 在不在特定頁面上時應用樣式

函數式編程 2023-03-18 17:46:44
我正在嘗試將一種樣式應用到我的導航欄,特別是當我在索引頁面以外的任何頁面上時,但我似乎無法讓它工作,我不確定問題是什么。這是我的腳本:$(document).ready(function(){  if (window.location.pathname != "") {    $('#main-nav').css('background-color', 'black');  }});如果重要的話,我正在使用 Angular 10。
查看完整描述

3 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

您沒有提到索引的路徑是什么。我猜它可能是“/”或“索引”。檢查您的索引路徑,然后比較代碼中的路徑


if (window.location.pathname != "" 

    || window.location.pathname != "/"

        || window.location.pathname != "index") {

            $('#main-nav').css('background-color', 'black');

}


查看完整回答
反對 回復 2023-03-18
?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

由于您使用的是 Angular 10,因此您可以使用路由器并添加樣式,這非常容易。

Angular Router 在你的 component.ts 文件中注入 Angular 路由器


Constructur(public router: Router){

  //After Logging check the url string and add it to the ngClass condition

  console.log(router.url);

}

然后在你的 html 文件中,在你的案例中,將 navbar html 文件添加到 navbar 標簽中,添加 ngClass。NgClass 指令


[ngClass]="{'customStyle': router.url=== '/'}"

在您的導航欄組件 css 文件中。


.customStyle{

background-color: "black"

}


查看完整回答
反對 回復 2023-03-18
?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

請試試這個:


$(document).ready(function(){

  if (window.location.pathname != "" || window.location.pathname.indexOf("index")>0) {

    $('#main-nav').css('background-color', 'black');

  }

});


查看完整回答
反對 回復 2023-03-18
  • 3 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號