是否可以在不破坏后代&#x27的情况下附加到Innerhtml。活动听众?
在以下示例代码中,我将 onclick
事件处理程序附加到包含文本“ foo”的跨度。处理程序是一个匿名功能,可弹出 alert()
。
但是,如果我分配给父节点的 Innerhtml
,则此 onclick
事件处理程序被销毁 - 单击“ foo”将无法弹出警报框。
这可以解决吗?
<html>
<head>
<script type="text/javascript">
function start () {
myspan = document.getElementById("myspan");
myspan.onclick = function() { alert ("hi"); };
mydiv = document.getElementById("mydiv");
mydiv.innerHTML += "bar";
}
</script>
</head>
<body onload="start()">
<div id="mydiv" style="border: solid red 2px">
<span id="myspan">foo</span>
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
.insertadjacenthtml()活动听众,
都得到了所有主要浏览器的支持。这是
.innerhtml
的简单单行替换。'trofennem'
参数指定插入HTML内容的元素中的位置。选项是'之前的'
,'autherbegin'
,'trofenend'和'rafterend'
。它们相应的位置是:Using
.insertAdjacentHTML()
preserves event listeners, and is supported by all major browsers. It's a simple one-line replacement for.innerHTML
.The
'beforeend'
argument specifies where in the element to insert the HTML content. Options are'beforebegin'
,'afterbegin'
,'beforeend'
, and'afterend'
. Their corresponding locations are:不幸的是,即使您试图附加,分配给
inninhtml
也会造成所有子元素的破坏。如果要保留子节点(及其活动处理程序),则需要使用:编辑:鲍勃的解决方案,来自评论。发布您的答案,鲍勃!为此获得信用。 :-)
Unfortunately, assignment to
innerHTML
causes the destruction of all child elements, even if you're trying to append. If you want to preserve child nodes (and their event handlers), you'll need to use DOM functions:Edit: Bob's solution, from the comments. Post your answer, Bob! Get credit for it. :-)
现在,是2012年,JQuery的附加和预端功能可以准确地添加内容,而不会影响当前内容。非常有用。
Now, it is 2012, and jQuery has append and prepend functions that do exactly this, add content without effecting current content. Very useful.
我创建了标记将其插入字符串,因为它的代码较少,并且更易于阅读,而不是使用Fancy Dom的东西。
然后,我将其变为临时元素的Innerhtml,这样我就可以抓住该元素的唯一子女并附着在身体上。
I created my markup to insert as a string since it's less code and easier to read than working with the fancy dom stuff.
Then I made it innerHTML of a temporary element just so I could take the one and only child of that element and attach to the body.
/代码> 可以使用。
或只是附加文本:
如果您不需要直接插入HTML,请考虑使用DOM方法创建结构:
在相关注释中,相应的
元素#prepend
方法可以在开始时插入元素(在第一个孩子之前)元素。Element#append
can be used to insert elements at the end of another element. To insert HTML from a string,Range#createContextualFragment
can be used.Or to just append text:
If you don't need to insert HTML directly, consider using DOM methods to create the structure instead:
On a related note, the corresponding
Element#prepend
method can be used to insert elements at the start (before the first child) of an element.还有另一种选择:使用
setAttribute
而不是添加事件侦听器。像这样:There is another alternative: using
setAttribute
rather than adding an event listener. Like this:是的,如果您使用tag属性绑定事件
onclick =“ sayhi()”
在模板中直接绑定事件,则可能是可能的 - 这种方法类似于框架/vue/vue/react/等。您还可以使用&lt; template&gt; template&gt;
在“动态” html上操作,例如在这里。它并不是严格的JS,但是它是可接受的小型项目
Yes it is possible if you bind events using tag attribute
onclick="sayHi()"
directly in template similar like your<body onload="start()">
- this approach similar to frameworks angular/vue/react/etc. You can also use<template>
to operate on 'dynamic' html like here. It is not strict unobtrusive js however it is acceptable for small projects