将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/