溫溫醬
2021-06-15 08:23:12
我想我的語法有問題。我想讓它說如果 url 的 indexof 425 或者 url 的 indexof 297 不要運行以下。這僅適用于做一個: if (document.location.href.indexOf('425') === -1){ 但是當(dāng)我嘗試添加第二個索引時它不起作用,這是我嘗試過的//attempt 1 if (document.location.href.indexOf('425') === -1 || document.location.href.indexOf('297') === -1){ }//attempt 2 if ((document.location.href.indexOf('425')) === -1 || (document.location.href.indexOf('297')) === -1)){ }
2 回答

波斯汪
TA貢獻1811條經(jīng)驗 獲得超4個贊
我想讓它說如果 url 的 indexof 425 或者 url 的 indexof 297 不要運行以下。
或者換一種說法,如果該URL不具有425和 不具有297,請執(zhí)行以下操作:
if (document.location.href.indexOf('425') === -1 && document.location.href.indexOf('297') === -1){
=== -1
表示沒有找到。
但是現(xiàn)在,您可以使用includes
(如果您需要支持 IE ,則可以使用IE 的 polyfilling):
if (!document.location.href.includes('425') && !document.location.href.includes('297')){

子衿沉夜
TA貢獻1828條經(jīng)驗 獲得超3個贊
你需要一個邏輯 AND&&,因為兩個部分都必須是true
if (
document.location.href.indexOf('425') === -1 &&
document.location.href.indexOf('297') === -1
) {
// ...
}
對于多個值,您可以使用包含不需要的部分的數(shù)組并Array#every用于檢查。
if ['425', '297'].every(s => !document.location.href.includes(s))) {
// ...
}
添加回答
舉報
0/150
提交
取消