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

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

问题描述

如何附加此HTML字符串

How to append this HTML string

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

给ID为$ code>'test'这是在DOM中?

to the DIV with the ID 'test' which is in the DOM?

(Btw div.innerHTML + = str; 是不可接受的。 / p>

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

推荐答案

如果可用,请使用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天全站免登陆