【问题标题】:Is there a way to remove attribute after calling `AddAttribute`调用`AddAttribute`后有没有办法删除属性
【发布时间】:2018-04-28 07:02:57
【问题描述】:

AddAttribute(msdn)添加属性后有什么方法可以删除属性吗?

例子:

public static void GenerateFieldInput(HtmlTextWriter writer)
{
    writer.RenderBeginTag(HtmlTextWriterTag.Input);
    writer.AddAttribute("placeholder", "some value");

    // some code logic

    writer.RemoveAttribute("placeholder"); // there isn't such method in HtmlTextWriter
}

【问题讨论】:

    标签: c# htmltextwriter


    【解决方案1】:

    HtmlTextWriter 和许多其他TextWriters 一样,只会将内容写入流中。没有正式的方法可以从中删除内容。

    您为什么要首先删除该属性?您是否在稍后的代码中发现不再需要该属性?如果是这种情况,请在编写之前尝试确定是否确实需要该标签。

    如果你不能这样做,你可以把你想要添加的所有属性放在一个List<T>,它允许你添加和删除元素。在你完全确定这就是你要写的内容之后,执行一个 foreach 循环并编写每个属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 2014-06-30
      • 2023-01-11
      • 2021-08-25
      • 1970-01-01
      相关资源
      最近更新 更多