问题描述
我正在寻找一种将包含 HTML 数据的字符串插入 div 元素的方法.该字符串是通过 XHR 加载的,无法知道其中包含哪些元素等.
I'm looking for a method to insert a string, which contains HTML data, into a div element. The string is loaded via XHR, and there is no way of knowing what elements and such are in it.
我四处搜索了一下,发现了一些可能会有所帮助,但对我来说并不完全有用的东西.我需要的是类似于 Prototype 框架中的 update() 函数的东西:http://prototypejs.org/api/element/update
I've searched around for a bit, and found some things that might help, but wouldn't completely work for me. What I need is something similar to the update() function from the Prototype framework: http://prototypejs.org/api/element/update
我正在编写的平台不允许使用框架或 JQuery.我被 Javascript 困住了.有人有什么想法吗?
The platform I'm writing for does not allow frameworks to be used, or JQuery. I'm stuck with Javascript. Anyone have any ideas?
我不能使用 innerHTML,因为它不会应用任何更新或功能,或者基本上任何应该在加载时发生的事情
I can't use innerHTML, as it does not apply any updates or functions or basically anything that's supposed to occur on load
我有一些需要发生的 onload
事件,据我所知,使用 innerHTML 不会执行 onload 事件.我错了吗?
I have some onload
events that need to occur, and as best I know, using innerHTML does not execute onload events. Am I incorrect?
2 年后对于其他阅读者,我对 onload
事件有一些严重的误解.我希望它对任何元素都是有效的事件,而它仅对 <body/>
元素有效..innerHTML
是我正在寻找的正确方法,添加元素的任何额外功能都需要以其他方式手动完成.
EDIT 2 years later:
For anyone else reading, I had some serious misconceptions about the onload
event. I expected that it was a valid event for any element, while it is only valid for the <body/>
element. .innerHTML
is the proper method to do what I was looking for, and any extra functionality for the elements added, needs to be done manually some other way.
推荐答案
HTMLElement innerHTML 属性
innerHTML 属性设置或返回元素的内部 HTML.
The innerHTML property sets or returns the inner HTML of an element.
HTMLElementObject.innerHTML=text
示例:http://jsfiddle.net/xs4Yq/
这篇关于将 HTML 作为字符串插入,不使用 JQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!