在 JavaScript 中为新添加的元素添加 onclick 事件
我一直在尝试将 onclick 事件添加到使用 JavaScript 添加的新元素中。
问题是当我检查 document.body.innerHTML 时,我实际上可以看到 onclick=alert('blah') 被添加到新元素中。
但是当我单击该元素时,我没有看到警报框正在工作。事实上,与 JavaScript 相关的任何内容都不起作用..
这是我用来添加新元素的方法:
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;
}
这是我调用此函数的方式:
<button onclick=add_img()>add image</button>
现在图像在浏览器中完美绘制。但是当我单击图像时,我没有收到该警报。
I have been trying to add onclick event to new elements I added with JavaScript.
The problem is when I check document.body.innerHTML I can actually see the onclick=alert('blah') is added to the new element.
But when I click that element I don't see the alert box is working. In fact anything related to JavaScript is not working..
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:
<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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
.onclick
应设置为函数而不是字符串。尝试一下。
.onclick
should be set to a function instead of a string. Tryinstead.
您还可以设置属性:
You can also set attribute:
不确定但尝试:
Not sure but try :
您不能通过字符串分配事件。使用它:
you can't assign an event by string. Use that:
简短的回答:您想将处理程序设置为一个函数:
稍微长一点的回答:您必须编写更多的代码行才能使其在浏览器中一致地工作。
事实是,即使是稍微长一些的代码可以在一组常见浏览器中解决特定问题,但它仍然会带来自己的问题。因此,如果您不关心跨浏览器支持,请选择完全短的。如果您关心它并且绝对只想让这一件事正常工作,请结合使用
addEventListener
和attachEvent
。如果您希望能够在整个代码中广泛创建对象并添加和删除事件侦听器,并且希望跨浏览器工作,那么您肯定希望将该责任委托给 jQuery 等库。Short answer: you want to set the handler to a function:
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
andattachEvent
. 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.我认为你不能这样做。您应该使用:
文档就在这里。
I don't think you can do that this way. You should use :
Documentation right here.
不能说为什么,但是 es5/6 语法不起作用
elem.onclick = (ev) => {console.log(this);}
不工作elem.onclick = function(ev) {console.log(this);}
工作cant say why, but the es5/6 syntax doesnt work
elem.onclick = (ev) => {console.log(this);}
not workingelem.onclick = function(ev) {console.log(this);}
working你有三个不同的问题。首先,HTML 标签中的值应该被引用!不这样做可能会使浏览器感到困惑,并可能导致一些麻烦(尽管这里的情况可能并非如此)。其次,您实际上应该为 onclick 变量分配一个函数,正如其他人所说的那样。这不仅是今后执行此操作的正确方法,而且如果您尝试在 onclick 函数中使用局部变量,它也会使事情变得更加简单。最后,您可以尝试 addEventListener 或 jQuery,jQuery 的优点是界面更好。
哦,还要确保您的 HTML 有效!这可能是一个问题。
You have three different problems. First of all, values in HTML tags should be quoted! 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. 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. Finally, you can try either addEventListener or jQuery, jQuery has the advantage of a nicer interface.
Oh, and make sure your HTML validates! That could be an issue.
jQuery:
或:
JQuery:
or: