问题描述
我正在寻找一种方法来将一个包含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()函数:
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.
推荐答案
HTMLElementObject.innerHTML=text
示例:
这篇关于将HTML作为字符串插入,不带JQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!