內嵌html字符串顯示
前端: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> & hello</div>';
document.getElementById( 'same' ).textContent =
'html === htmlDecode( htmlEncode( html ) ): '
+ ( html === htmlDecode( htmlEncode( html ) ) );