我想為包含某個 url 的頁面的標題欄創建一個類。我的網址是www.domainname.com/poste/XXX,只有在 .com 之后的部分僅包含 /poste/ 時,我才想添加該類。我見過“包含這個詞”的例子,但這并不是我要找的。標題欄的現有類是: .fusion-page-title-bar 在此先感謝您的幫助和建議!
uj5u.com熱心網友回復:
要檢查 url 是否包含任何字串,請執行以下操作:
if(window.location.href.indexOf(".com/poste") > -1) {
// do something
}
(檢查字串的索引是否大于 -1 就像問他是否在里面)
有條件地添加類:
element.classList.add("my-class");
結合起來將是:
if(window.location.href.indexOf(".com/poste") > -1) {
titleClass = document.querySelector(".your-title-class");
titleClass.classList.add("conditionalClass");
}
*還有其他使用 jquery 的解決方案(如@Wimanicesir 評論中的那個),但它個人更喜歡不使用它:)
uj5u.com熱心網友回復:
如果我理解您的問題是您想檢查 URL 是否以 /poste/結尾。
您可以使用字串函式endsWith()。
var url = window.location.href
console.log('Current url: ', url)
if (url.endsWith("js")) {
console.log('The url ends with js');
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/428603.html
標籤:javascript css 网址 标题栏
上一篇:處理PHPURL和GET請求
