我想在事件发生后停止条件JavaScript

时间:2018-09-24 09:37:28

标签: javascript html events addeventlistener

(我的意思是,当某人单击按钮一次更改或创建一个元素时,请知道它将以另一种方式工作,但是我需要知道是否可以通过这种方式解决它。...感谢您的支持)

const BAG = document.querySelector('#container');
//const SMS = document.querySelector('#sms');
// const DEL = document.querySelector('#del');
BAG.addEventListener('click', function(apply) {
  var target = apply.target;

  if (target.id === 'sms') {
    console.log('Hi, Someone hit the Botton CLICK HERE');
  }
} else if (target.id === "del") {
  console.log('DELETE botton was clecked');
} else {
  console.log('^BODY was clecked');
}
}, false);
body {
  text-align: center;
  margin-top: 250px;
  font-family: Tahoma;
}
<div id="container">
  <h1>Hello!! Please click on the Botton Below</h1>
  <button id="sms">Click Here</button>
  <button id="del">Delete the Message</button>
</div>

1 个答案:

答案 0 :(得分:1)

您已添加了额外的{,按代码无法正常使用...更新了以下代码:

var bag = document.querySelector('#container');
//const SMS = document.querySelector('#sms');
//const DEL = document.querySelector('#del');
bag.addEventListener('click', function(apply) {
  var target = apply.target;
  if (target.id === 'sms') {
console.log('Hi, Someone hit the Botton CLICK HERE');
target.setAttribute('disabled','disabled');
  }

 else if (target.id === "del") {
  console.log('DELETE botton was clecked');
  target.setAttribute('disabled','disabled');
} else {
  console.log('^BODY was clecked');
}
});
<div id="container">
  <h1>Hello!! Please click on the Botton Below</h1>
  <button id="sms">Click Here</button>
  <button id="del">Delete the Message</button>
</div>