問題描述
我正在尋找一種將包含 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!