简体   繁体   English

将 OnClick Function 添加到新创建的映像中

[英]Add OnClick Function to newly created image that

I'm needing to dynamicly add a onclick that can focus on itself so when clicked it will add the width and remove it我需要动态添加一个 onclick 可以专注于自身,所以当点击它会添加宽度并删除它

function addimage2(where) { 
  var img = document.createElement("img");
  img.src = "swatch.jpg";  
  img.width = 300;
  where.parentNode.appendChild(img);
}

old method旧方法

<img src="swatch.jpg" alt="swatch-sm" onClick="addimage2(this);">

I need it like this as new images are created dynamicly我需要这样,因为新图像是动态创建的

img.onclick = function(this){
   if(this.width == 300) {
      this.removeAttribute("width");
   }else{
      this.setAttribute("width","300");
   }
}

Simply say简单地说

img.addEventListener("click",addimage2);

If you need to pass parameters to addimage2 then use the following:如果您需要向addimage2传递参数,请使用以下命令:

img.addEventListener("click",event => {
    addimage2(parameters,here);
});

If you're going to be adding a bunch of these things, may I suggest a single delegated event listener, then you can add more images later and they will "just work".如果您要添加一堆这些东西,我可以建议一个委托事件侦听器,然后您可以稍后添加更多图像,它们将“正常工作”。 All you need to do is assign a class to all of the images you want to treat this way.您需要做的就是将 class 分配给您要以这种方式处理的所有图像。

 document.addEventListener("click", (e) => { const el = e.target; if (el.classList.contains("resizey")) { if (el.width == 300) { el.removeAttribute("width"); } else { el.setAttribute("width","300"); } } }, true); // true to use capture
 <img class='resizey' src='https://upload.wikimedia.org/wikipedia/commons/3/3f/A_small_loch_in_the_saddle_between_Beinn_an_Dothaidh_and_Beinn_Dorain%2C_Scotland_01.jpg' /> <img class='resizey' src='https://upload.wikimedia.org/wikipedia/commons/9/94/Techumbre_mud%C3%A9jar%2C_Catedral%2C_Teruel%2C_Espa%C3%B1a%2C_2014-01-10%2C_DD_38.JPG' />

暂无
暂无

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

相关问题 动态创建的图像中的onclick函数不起作用 - onclick function in an image which is dynamically created is not working 如何在 JS 或 Jquery 中向新创建的按钮添加 onclick 事件? - How can I add an onclick event to a newly created button in JS or Jquery? 将onclick事件添加到javascript中新创建的表格标题中,但是onclick函数被添加到页面上的所有内容中,而不仅仅是我想要的一个 - Adding onclick event to newly created table header in javascript, but the onclick function is added to all th's on the page, not just the one I want 将函数绑定到新创建的元素 - bind function to newly created element 在新创建的元素上绑定功能 - Bind function on newly created element 将 onclick 事件添加到 JavaScript 中新添加的元素 - Add onclick event to newly added element in JavaScript 尝试将属性“onclick”添加到由javascript函数创建的html“img”元素 - Trying to add attribute “onclick” to a html “img” element created by javascript function 如何使用纯Javascript向动态创建的div添加onclick函数 - How to add an onclick function to dynamically created divs with pure Javascript 如何在活动(按下)状态下为新创建的html按钮添加背景图片? - How to add background image for newly created html button in active(pressed) state? DOM:在onclick事件中删除新创建的“article”元素并使用新创建的删除按钮? - DOM: Delete newly created 'article' element with newly created delete button within onclick event?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM