Insert HTML as a String, without JQuery(将 HTML 作为字符串插入,不使用 JQuery)
问题描述
我正在寻找一种将包含 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 HTML 作为字符串插入,不使用 JQuery
基础教程推荐
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
