javascript 将特殊字符转换为 Html 编码字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17106949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:07:10  来源:igfitidea点击:

Convert Special characters into Html Encoded characters

c#javascriptjqueryspecial-characters

提问by Ishan Jain

I want to converter all special characters into Html encoded characters. I found many post related to used HttpUtility.HtmlEncode();, but it's only convert some of special characters like "&", "<", ">".

我想将所有特殊字符转换为 Html 编码的字符。我发现了很多与 used 相关的帖子HttpUtility.HtmlEncode();,但它只是转换了一些特殊字符,如"&", "<", ">".

Is there any way to convert all special character like "?","?","t","?","ù"into Html entity using C# or javascript?

有没有办法"?","?","t","?","ù"使用 C# 或 javascript将所有特殊字符转换为 Html 实体?

回答by Alex K.

The Microsoft AntiXss Librarycan accomplish this;

微软AntiXss图书馆可以做到这一点;

string p = Microsoft.Security.Application.Encoder.HtmlEncode("aaa <b>sdf</b> ?,?,t,?,ù", true);
Response.Write(p);

For

为了

aaa &lt;b&gt;sdf&lt;/b&gt; &scaron;,&Oslash;,&thorn;,&rsaquo;,&Ugrave;

回答by Shahyad Sharghi

you can also do as follows without AntiXSS

您也可以在没有 AntiXSS 的情况下执行以下操作

public static string HtmlEncode (string text)
{
    string result;
    using (StringWriter sw = new StringWriter())
    {
        var x = new HtmlTextWriter(sw);
        x.WriteEncodedText(text);
        result = sw.ToString();
    }
    return result;

}

回答by Suresh Atta

Yes.Using javascript Escape them.

Yes.Using javascript转义它们

document.write(escape("3423424242<><><$$"));