【问题标题】:c# How to format string for use in HTML attribute?c#如何格式化字符串以用于HTML属性?
【发布时间】:2016-06-25 04:06:07
【问题描述】:

我想知道是否有任何 c# 库可以轻松格式化字符串以用于 onclick html 属性属性。

我正在使用 c# 动态生成一个 html 页面,并为一些 onclick 事件动态生成 javascript。我想确保诸如 &"\ ... 等字符不会导致 html 中断或被忽略。例如在下面的示例中,我想显示警报消息 h\m,我不会能够这样做,除非我创建了单独的逻辑来处理 \ 和 &:

function method1(inputValue){
  alert(inputValue);
  }
<input type="button" value="button1" onclick="method1('h\&#3m')"/>

【问题讨论】:

    标签: c# html string-formatting


    【解决方案1】:

    您可能想查看 HttpServerUtility 的 HtmlEncode 方法。 https://msdn.microsoft.com/en-us/library/w3te6wfz(v=vs.110).aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 2016-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多