显示范围或htmlfrag的html内容
创建范围后,如何在 Firefox 中显示它的 HTML 内容? range.toString()
只给我文本内容。本质上,就像innerHTML返回IE中的标记一样。 谢谢
function innerHTML(oTarget, sContent, bAppend){
if (document.getElementById && !document.all) {//is this a non ie browser
var range = document.createRange();
range.setStart(oTarget, 0);
range.setEnd(oTarget, oTarget.childNodes.length);
if (sContent) {//if there is content, save it if not return the content
var htmlFrag = range.createContextualFragment(sContent);
if (bAppend != true) {//append or replace
range.deleteContents();
}
oTarget.appendChild(htmlFrag); //add the new html
} else {
sContent = range.toString();
}
} else {
if (sContent) {
if (bAppend == true) {
oTarget.innerHTML += sContent;
} else {
oTarget.innerHTML = sContent;
}
}
sContent = oTarget.innerHTML;
}
return sContent;
}
Once I create a range how do I display the HTML content of it in Firefox?range.toString()
only gives me the text content. In essence, like innerHTML returns the markup in IE.
Thanks
function innerHTML(oTarget, sContent, bAppend){
if (document.getElementById && !document.all) {//is this a non ie browser
var range = document.createRange();
range.setStart(oTarget, 0);
range.setEnd(oTarget, oTarget.childNodes.length);
if (sContent) {//if there is content, save it if not return the content
var htmlFrag = range.createContextualFragment(sContent);
if (bAppend != true) {//append or replace
range.deleteContents();
}
oTarget.appendChild(htmlFrag); //add the new html
} else {
sContent = range.toString();
}
} else {
if (sContent) {
if (bAppend == true) {
oTarget.innerHTML += sContent;
} else {
oTarget.innerHTML = sContent;
}
}
sContent = oTarget.innerHTML;
}
return sContent;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
innerHTML
是 JavaScript 标准的一部分,所有现代浏览器都支持。只要oTarget
是 HTML 元素对象,您的else
语句(对于 IE)也应该在 Firefox 中工作。innerHTML
is part of the JavaScript standard and is supported by all modern browsers. Yourelse
statement (for IE) should work in Firefox as well, as long asoTarget
is an HTML element object.