在 JavaScript 中为新添加的元素添加 onclick 事件

发布于 2024-09-11 13:34:00 字数 775 浏览 1 评论 0原文

我一直在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

记忆里有你的影子 2024-09-18 13:34:00

.onclick 应设置为函数而不是字符串。尝试

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

一下。

.onclick should be set to a function instead of a string. Try

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

instead.

铜锣湾横着走 2024-09-18 13:34:00

您还可以设置属性:

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

You can also set attribute:

elem.setAttribute("onclick","alert('blah');");
东京女 2024-09-18 13:34:00

不确定但尝试:

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

Not sure but try :

elemm.addEventListener('click', function(){ alert('blah');}, false);
优雅的叶子 2024-09-18 13:34:00

您不能通过字符串分配事件。使用它:

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

you can't assign an event by string. Use that:

elemm.onclick = function(){ alert('blah'); };
潦草背影 2024-09-18 13:34:00

简短的回答:您想将处理程序设置为一个函数:

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

稍微长一点的回答:您必须编写更多的代码行才能使其在浏览器中一致地工作。

事实是,即使是稍微长一些的代码可以在一组常见浏览器中解决特定问题,但它仍然会带来自己的问题。因此,如果您不关心跨浏览器支持,请选择完全短的。如果您关心它并且绝对只想让这一件事正常工作,请结合使用 addEventListenerattachEvent。如果您希望能够在整个代码中广泛创建对象并添加和删除事件侦听器,并且希望跨浏览器工作,那么您肯定希望将该责任委托给 jQuery 等库。

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. 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.

情话难免假 2024-09-18 13:34:00

我认为你不能这样做。您应该使用:

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

文档就在这里

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.

苍景流年 2024-09-18 13:34:00

不能说为什么,但是 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 working

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

路还长,别太狂 2024-09-18 13:34:00

你有三个不同的问题。首先,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.

橙味迷妹 2024-09-18 13:34:00

jQuery:

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

或:

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

JQuery:

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

or:

elemm.attr("onclick", "alert('Hi!')");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文