简体   繁体   English

为什么当我使用 addEventListener 时复选框停止工作

[英]why does checkbox stops working when ever i use addEventListener

when ever i use addEventListener my check box stops working i tried removing every code but when i add this code my checkbox stops working this is my event listener当我使用addEventListener我的复选框停止工作我尝试删除每个代码但是当我添加此代码时我的复选框停止工作这是我的事件监听器

document.addEventListener('click', function(e) {
  e.preventDefault();
});

my document has innerhtml before it and i wanted to get the selected items id this is the innerhtml我的文档之前有innerhtml ,我想获取选定的项目 id 这是innerhtml

document.innerHTML += "<div class='product-container'><div class='course'><div class='course-preview'><img class='product-image' src="+productimageUrl+"></div><div class='course-info'><div class='progress-container'><h6>OnSale: "+ productsale +"</h6><h6>Featured: "+productfeatured +"</h6><h6>soldOut: "+ productsoldout +"</h6></div><h2>"+ productname +" "+ productprice +" </h2><h4>"+productcategory+", "+productsubcategory+"</h6><h4>"+ productamount +"</h4><h6>PRODUCT ID:</h6><h5>"+ productid +"</h5><button id='editbutton' class='btn'>Edit Product</button></div></div></div>"    

whenever i use this code this code it stops working in case you want document this is my document每当我使用此代码时,此代码就会停止工作,以防您想要文档,这是我的文档

const document = document.getElementById("main-page");

e.preventDefault(); prevents any action that would've taken place when clicking.防止单击时发生的任何操作。

If you want to preventDefault() only when you're not clicking on a checkbox, test if the target object is or not an input.如果您只想在单击复选框时阻止Default(),请测试目标 object 是否为输入。

document.addEventListener('click', function(e) {
  if(e.target.tagName != "INPUT") {
    e.preventDefault();
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM