追加HTML字符串到DOM [英] Appending HTML string to the DOM

查看:275
本文介绍了追加HTML字符串到DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何追加这个HTML字符串

How to append this HTML string

var str = '<p>Just some <span>text</span> here</p>';

与ID的DIV 测试这是DOM?

(顺便说一句 div.innerHTML + = str中。是不能接受的)

(Btw div.innerHTML += str; is not acceptable.)

推荐答案

使用insertAdjacentHTML如果可用,否则使用某种回退。 insertAdjacentHTML将在Firefox 8的支持,而且还支持在Safari 4,Chrome浏览器,IE和Opera,根据<一个href=\"https://developer.mozilla.org/en/DOM/element.insertAdjacentHTML\">https://developer.mozilla.org/en/DOM/element.insertAdjacentHTML

Use insertAdjacentHTML if it's available, otherwise use some sort of fallback. insertAdjacentHTML will be supported in Firefox 8, and is also supported in Safari 4, Chrome, IE and Opera, according to https://developer.mozilla.org/en/DOM/element.insertAdjacentHTML

div.insertAdjacentHTML( 'beforeend', str );

现场演示: http://jsfiddle.net/euQ5n/

这篇关于追加HTML字符串到DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆