javascript ASP.Net 控件的 InnerHTML 和 InnerText 属性之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19413642/
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 15:30:24  来源:igfitidea点击:

Difference between InnerHTML and InnerText property of ASP.Net controls?

c#javascripthtmlasp.net

提问by techfun

While using ASP.NET controls, for example

例如,在使用 ASP.NET 控件时

<h1 id="header" runat="server">text</h1>

if we want to change the text of the header we can do it probably by two properties InnerHTMLand InnerText. I want to know what is the basic difference between the two properties?

如果我们想更改标题的文本,我们可能可以通过两个属性InnerHTMLInnerText. 我想知道这两个属性的基本区别是什么?

回答by Yandros

InnerHtmllets you enter HTML code directly, InnerTextformats everything you put in there for it to be taken as plain text.

InnerHtml允许您直接输入 HTML 代码,InnerText格式化您放入其中的所有内容,以便将其视为纯文本。

For example, if you were to enter this in both properties: Hello <b>world</b>

例如,如果您要在两个属性中输入: Hello <b>world</b>

This is what you would get with InnerHTML:

这就是使用 InnerHTML 会得到的结果:

Hello world

你好世界

That is, exactly the same HTML you entered.

也就是说,与您输入的 HTML 完全相同。

Instead, if you use InnerText, you get this:

相反,如果你使用InnerText,你会得到这个:

Hello <b>world</b>

Hello <b>world</b>

And the resulting HTML would be Hello &lt;b&gt;world&lt;/b&gt;

生成的 HTML 将是 Hello &lt;b&gt;world&lt;/b&gt;

回答by Tim Medora

When in doubt, go to the source (or decompile):

如有疑问,请转到源(或反编译):

In HtmlContainerControl:

HtmlContainerControl

public virtual string InnerText
{
    get
    {
        return HttpUtility.HtmlDecode(this.InnerHtml);
    }
    set
    {
        this.InnerHtml = HttpUtility.HtmlEncode(value);
    }
}

public virtual string InnerHtml
{
    get
    {
        if (base.IsLiteralContent())
        {
            return ((LiteralControl)this.Controls[0]).Text;
        }
        if (this.HasControls() && this.Controls.Count == 1 && this.Controls[0] is DataBoundLiteralControl)
        {
            return ((DataBoundLiteralControl)this.Controls[0]).Text;
        }
        if (this.Controls.Count == 0)
        {
            return string.Empty;
        }
        throw new HttpException(SR.GetString("Inner_Content_not_literal", new object[]
        {
            this.ID
        }));
    }
    set
    {
        this.Controls.Clear();
        this.Controls.Add(new LiteralControl(value));
        this.ViewState["innerhtml"] = value;
    }
}

Both properties ultimately use InnerHtml, but setting InnerTextHTML encodes the value so that it will be displayed literally in the browser versus interpreted as markup.

这两个属性最终都使用InnerHtml,但设置InnerTextHTML 会对该值进行编码,以便它在浏览器中按字面显示而不是解释为标记。

Remember that assigning to InnerHtmlwill not encode the value, and thus any user-driven content should be sanitized prior to assignment.

请记住,分配给InnerHtml不会对值进行编码,因此任何用户驱动的内容都应在分配之前进行清理。

This also emphasizes how important it is to be mindful of view state (note the last line of InnerHtml's setter; everything ends up in view state whether or not you need it).

这也强调了注意视图状态的重要性(注意InnerHtml's setter的最后一行;无论您是否需要,一切都以视图状态结束)。

回答by Gonzix

InnerHtml allows to insert html formated text within an HTML container, while InnerText only allows plain text (if I remember correctly this property trims any type of html you try to put in it)

InnerHtml 允许在 HTML 容器中插入 html 格式的文本,而 InnerText 只允许纯文本(如果我没记错的话,此属性会修剪您尝试放入的任何类型的 html)

  1. InnerHtml. http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlcontainercontrol.innerhtml.aspx
  2. InnerText. http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlcontainercontrol.innertext.aspx
  1. 内部 Html。http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlcontainercontrol.innerhtml.aspx
  2. 内文。http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlcontainercontrol.innertext.aspx