內嵌html字符串顯示

来源:互联网 发布:男人眼中的女人味知乎 编辑:程序博客网 时间:2024/05/01 01:41

前端:System.Web.HttpUtility.HtmlEncode()
           @Html.Raw(htmlStr)
後端:System.Net.WebUtility.HtmlDecode(UserName)

Jquery實現
http://jsfiddle.net/ThinkingStiff/FSaU2/

 

function htmlEncode( html ) {
    return document.createElement( 'a' ).appendChild(
        document.createTextNode( html ) ).parentNode.innerHTML;
};

function htmlDecode( html ) {
    var a = document.createElement( 'a' ); a.innerHTML = html;
    return a.textContent;
};

document.getElementById( 'text' ).value = htmlEncode( document.getElementById( 'hidden' ).value );

//sanity check
var html = '<div>   &amp; hello</div>';
document.getElementById( 'same' ).textContent =
      'html === htmlDecode( htmlEncode( html ) ): '
    + ( html === htmlDecode( htmlEncode( html ) ) );

 

 

 

 

0 0
原创粉丝点击