从一个div元素中使用JavaScript移除一个onclick事件

huangapple go评论63阅读模式
英文:

Removing an onclick event from a div with javascript

问题

<div id="box" onclick="doSmt(var1, bar2)">
if (condition){
box.removeEventListener("click" , doSmt)}
英文:

Basically what the title says this is the code that I've tried but it doesn't work:

<div id="box" onclick="doSmt(var1, bar2)">
if (condition){
box.removeEventListener("click" , doSmt)}

答案1

得分: 2

我认为最好的方法是删除onclick事件,而不是那个尝试

//onclick function
function doSmt(){
   console.log("something");
}

//remove onclick event, this will be inside your if condition
document.getElementById('box').removeAttribute("onclick");
<div id="box" onclick="doSmt()"> div</div>
英文:

I think it's better if you remove the onclick event instead of that attempt

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

//onclick function
function doSmt(){
   console.log(&quot;something&quot;);
}

//remove onclick event, this will be inside your if condition
document.getElementById(&#39;box&#39;).removeAttribute(&quot;onclick&quot;);

<!-- language: lang-html -->

&lt;div id=&quot;box&quot; onclick=&quot;doSmt()&quot;&gt; div&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

根据MDN关于 removeEventListener 的文档,你无法移除作为HTML属性一部分的事件监听器。所以有两种选项:

  1. 在页面加载时添加事件监听器:

    onClickHandler = () => doSmt(var1, var2);
    document.addEventListener('DOMContentLoaded', () => {
      document.getElementById('box').addEventListener('click', onClickHandler);
    });
    
    // 稍后
    if (condition) {
      document.getElementById('box').removeEventListener('click', onClickHandler);
    }
    
  2. 如果你无法修改HTML,你可以修改 doSMT 来检查是否已禁用:

    let disableBox = false;
    
    function doSmt() {
      if (disableBox) return;
      // ...
    }
    
    if (condition) {
      disableBox = true;
    }
    

或者,你可以通过首先访问元素,然后将属性设置为null来移除它:

<div id="myDiv" onclick="handleClick()">Click me</div>

<script>
  function handleClick() {
    alert("Div was clicked!");
  }

  // 从div中移除onclick事件
  const div = document.getElementById("myDiv");
  div.onclick = null;
</script>
英文:

What what I read at MDN for removeEventListener you can't remove an event listener that is part of the HTML attribute. So there's two options:

Add the event listener on page load

onClickHandler = () =&gt; doSmt(var1, var2);
document.addEventListener(&#39;DOMContentLoaded&#39;, () =&gt; {
  document.getElementById(&#39;box&#39;).addEventListener(&#39;click&#39;, onClickHandler);
});

// and later
if (condition) {
document.getElementById(&#39;box&#39;).removeEventListener(&#39;click&#39;, onClickHandler)

Or if you can't modify the HTML you could modify doSMT to watch for a disabled bit.

let disableBox = false;

function doSmt() {
  if (disableBox) return;
  // ...
}

if (condition) {
  disableBox = true;
}

Or

it can be removed by first accessing the element and then setting the attribute to null

&lt;div id=&quot;myDiv&quot; onclick=&quot;handleClick()&quot;&gt;Click me&lt;/div&gt;

&lt;script&gt;
  function handleClick() {
    alert(&quot;Div was clicked!&quot;);
  }

  // Remove the onclick event from the div
  const div = document.getElementById(&quot;myDiv&quot;);
  div.onclick = null;
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年2月7日 02:30:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定