簡體   English   中英

如果條件為真或跳過所有其他條件

[英]if condition is true or skip all else conditions

xyz.html

<p id = x ngClick = close($event)></p>
<p id = y ngClick = close($event)></p>
<p id = z ngClick = close($event)></p>
<h1 id = a ngClick = close()></h1>

xyz.js

function close(event)
if (event.target.id === x){
....
}else if(event.target.id === y){
....
}else if(event.target.id === z){
.....
}

此功能是關閉當前窗口並檢查事件,但是當我沒有在其他關閉方法(h1標簽)中傳遞此事件時,得到的事件是未定義錯誤。

錯誤是正確的。 您需要首先檢查事件是否已定義,因為如果事件未定義,則無法直接訪問目標屬性。

function close(event)
   if(event){
    if (event.target.id === x){
    ....
    }else if(event.target.id === y){
    ....
    }else if(event.target.id === z){
    .....
    }
    }else{
           //H1 click condition
      }
}
 function close(event){
     if(event===undefined){

     //H1 got clicked
       return;
     }

     if (event.target.id === x){
     ....
     }else if(event.target.id === y){
     ....
     }else if(event.target.id === z){
     .....
     }
   }

嘗試這個:

$scope.close = function(event) {
  if (!event) return;

  switch (event.target.id) {
    case "x":
      ...
      break;
    case "y":
      ...
      break;
    case "z":
      ...
      break;
    default:
      ...
      break;
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM