简体   繁体   English

如果条件为真或跳过所有其他条件

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

xyz.html 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 xyz.js

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

This function is to close current window and check for the event but when I'm not passing this event in other close methods(h1 tag) I'm getting the event is undefined error. 此功能是关闭当前窗口并检查事件,但是当我没有在其他关闭方法(h1标签)中传递此事件时,得到的事件是未定义错误。

The error is correct. 错误是正确的。 You need first check if event is defined, because you can't directly access target property if event is undefined. 您需要首先检查事件是否已定义,因为如果事件未定义,则无法直接访问目标属性。

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){
     .....
     }
   }

Try this: 尝试这个:

$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