简体   繁体   English

如何将事件添加到使用JS创建的元素?

[英]How to add event to element created with js?

I created the script below. 我在下面创建了脚本。 It puts the body in a div, as desired. 根据需要将其放入div中。 However, the event handler fails. 但是,事件处理程序失败。 I'm sure it's a simple error. 我敢肯定这是一个简单的错误。

body_html = document.body.innerHTML;
new_html = "<div id='realBody'>" + body_html + "</div>";
document.body.innerHTML = new_html;
document.body.getElementById("realBody").addEventListener("mouseover", function(event) {alert('body');});

How to make the event work? 如何使活动进行? (Feel free to rewrite the whole thing if there's a better (simpler) way. (如果有更好(更简单)的方法,请随意重写整个内容。

Edit: This works, some comments say it's the wrong way to put the body into a div, but i see no problems: 编辑:这有效,一些评论说这是将主体放入div的错误方法,但我认为没有问题:

body_html = document.body.innerHTML;
new_html = "<div id='realBody'>" + body_html + "</div>";
document.body.innerHTML = new_html;
document.getElementById("realBody").addEventListener("mouseenter", function(event) {alert('body');});

thx! 谢谢!

You should attach event listener like, 您应该附加事件监听器,例如

body_html = document.body.innerHTML;
new_html = "<div id='realBody'>" + body_html + "I am border</div>";
document.body.innerHTML = new_html;
document.getElementById("realBody").addEventListener("mouseover", function (event) {
    alert('body');
});

DEMO 演示

您需要将document.body.getElementById()更改为document.getElementById()

document.getElementById("realBody").addEventListener("mouseover", function(event) {alert('body');});

The error you're receiving is probably because of this line: 您收到的错误可能是由于以下原因:

document.body.getElementById("realBody").addEventListener("mouseover", function (event) { alert('body'); });

you need to modify it to this: 您需要对此进行修改:

document.getElementById("realBody").addEventListener("mouseover", function (event) { alert('body'); });

document.body.getElementById() just needs to be document.getElementByid() document.body.getElementById()只需是document.getElementByid()

A more proper way to add elements to the DOM would be to do something like below: 将元素添加到DOM的更合适的方法是执行以下操作:

var divEl = document.createElement("div");
divEl.id = "realBody";
var textNode = document.createTextNode("Hover over me");
divEl.appendChild(textNode);
document.body.appendChild(divEl);

divEl.onmouseover = function (event) {
    alert('body');
}

As everybody explained, you need to call the getElementById() on the document object, not in an element. 正如大家所解释的,您需要在文档对象上而不是在元素中调用getElementById()。

But your code will loose any event handlers attached to the elements prior to your code execution as you are clearing the html and creating a new dom structure you overwritting the innerHTML. 但是,当您清除html并覆盖了innerHTML的情况下创建新的dom结构时,您的代码将在执行代码之前松动附加到元素的所有事件处理程序。

Instead just move the existing dom to the new element like 而是将现有dom移至新元素,例如

var wrapper = document.createElement('div');
wrapper.id = 'realBody';
while (document.body.firstChild) {
    wrapper.appendChild(document.body.firstChild);
}
document.body.appendChild(wrapper);
wrapper.addEventListener("mouseover", function (event) {
    console.log('body');
});

Demo: Fiddle 演示: 小提琴

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

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