简体   繁体   English

将 onclick 事件添加到 JavaScript 中新添加的元素

[英]Add onclick event to newly added element in JavaScript

I have been trying to add onclick event to new elements I added with JavaScript.我一直在尝试将 onclick 事件添加到我使用 JavaScript 添加的新元素中。

The problem is when I check document.body.innerHTML I can actually see the onclick=alert('blah') is added to the new element.问题是当我检查 document.body.innerHTML 时,我实际上可以看到 onclick=alert('blah') 已添加到新元素中。

But when I click that element I don't see the alert box is working.但是当我单击该元素时,我看不到警报框正在工作。 In fact anything related to JavaScript is not working..事实上,与 JavaScript 相关的任何事情都不起作用..

here is what I use to add new element:这是我用来添加新元素的方法:

function add_img() { 
  var elemm = document.createElement('rvml:image'); 
  elemm.src = 'blah.png';
  elemm.className = 'rvml';
  elemm.onclick = "alert('blah')";
  document.body.appendChild(elemm);
  elemm.id = "gogo";
  elemm.style.position='absolute';
  elemm.style.width=55;
  elemm.style.height=55;
  elemm.style.top=200;
  elemm.style.left=300;
  elemm.style.rotation=200; 
}

Here is how I call this function:这是我如何称呼这个 function:

<button onclick=add_img()>add image</button>

Now the image draws perfectly inside the browser.现在图像在浏览器中完美绘制。 But when I click the image I don't get that alert.但是当我单击图像时,我没有收到该警报。

.onclick should be set to a function instead of a string. .onclick应该设置为函数而不是字符串。 Try尝试

elemm.onclick = function() { alert('blah'); };

instead.反而。

您还可以设置属性:

elem.setAttribute("onclick","alert('blah');");

不确定,但尝试:

elemm.addEventListener('click', function(){ alert('blah');}, false);

you can't assign an event by string.您不能按字符串分配事件。 Use that:使用那个:

elemm.onclick = function(){ alert('blah'); };

Short answer: you want to set the handler to a function:简短回答:您想将处理程序设置为函数:

elemm.onclick = function() { alert('blah'); };

Slightly longer answer: you'll have to write a few more lines of code to get that to work consistently across browsers.稍微长一点的答案:你必须多写几行代码才能让它在浏览器中一致地工作。

The fact is that even the sligthly-longer-code that might solve that particular problem across a set of common browsers will still come with problems of its own.事实是,即使可能在一组常见浏览器中解决特定问题的稍长代码仍然会带来其自身的问题。 So if you don't care about cross-browser support, go with the totally short one.因此,如果您不关心跨浏览器支持,请选择完全简短的支持。 If you care about it and absolutely only want to get this one single thing working, go with a combination of addEventListener and attachEvent .如果您关心它并且绝对只想让这件事起作用,请结合使用addEventListenerattachEvent If you want to be able to extensively create objects and add and remove event listeners throughout your code, and want that to work across browsers, you definitely want to delegate that responsibility to a library such as jQuery.如果您希望能够在整个代码中广泛地创建对象并添加和删除事件侦听器,并且希望它可以跨浏览器工作,那么您肯定希望将该职责委托给一个库,例如 jQuery。

I don't think you can do that this way.我不认为你可以这样做。 You should use :你应该使用:

void addEventListener( 
  in DOMString type, 
  in EventListener listener, 
  in boolean useCapture 
); 

Documentation right here .文档 就在这里

You have three different problems.你有三个不同的问题。 First of all, values in HTML tags should be quoted!首先,HTML 标签中的值应该被引用! Not doing this can confuse the browser, and may cause some troubles (although it is likely not the case here).不这样做会混淆浏览器,并可能导致一些麻烦(尽管这里可能不是这种情况)。 Second, you should actually assign a function to the onclick variable, as someone else meantioned.其次,您应该像其他人所说的那样,为 onclick 变量分配一个函数。 Not only is this the proper way to do it going forward, but it makes things much simpler if you are trying to use local variables in the onclick function.这不仅是前进的正确方法,而且如果您尝试在 onclick 函数中使用局部变量,它会使事情变得更简单。 Finally, you can try either addEventListener or jQuery, jQuery has the advantage of a nicer interface.最后,您可以尝试 addEventListener 或 jQuery,jQuery 的优点是界面更好。

Oh, and make sure your HTML validates!哦,并确保您的 HTML 验证! That could be an issue.那可能是个问题。

JQuery:查询:

elemm.attr("onclick", "yourFunction(this)");

or:或者:

elemm.attr("onclick", "alert('Hi!')");

cant say why, but the es5/6 syntax doesnt work不能说为什么,但 es5/6 语法不起作用

elem.onclick = (ev) => {console.log(this);} not working elem.onclick = (ev) => {console.log(this);}不工作

elem.onclick = function(ev) {console.log(this);} working elem.onclick = function(ev) {console.log(this);}工作

In case you do not want to write all the code you have once written in the function you called.如果您不想在您调用的函数中编写您曾经编写的所有代码。 Please use the following code, using jQuery:请使用以下代码,使用 jQuery:

$(element).on('click', function () { add_img(); }); $(element).on('click', function () { add_img(); });

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

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