开发者

Display html content of range or htmlfrag

开发者 https://www.devze.com 2022-12-16 21:17 出处:网络
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.

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.deleteConte开发者_StackOverflownts();
        }
        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;
}


innerHTML is part of the JavaScript standard and is supported by all modern browsers. Your else statement (for IE) should work in Firefox as well, as long as oTarget is an HTML element object.

0

精彩评论

暂无评论...
验证码 换一张
取 消