【问题标题】:Put quote into a value attribute html [duplicate]将引号放入值属性html [重复]
【发布时间】:2013-04-14 19:39:54
【问题描述】:

给定一个表单输入字段,例如<input type="text" value="xxxxx" name="something"> 给定一个字符串让我们说Hello I said "Your my friend" isn't that nice?

我如何安全地输入给定的字符串作为上面输入标签中“xxxxx”的值?

直接替换会导致: <input type="text" value="Hello I said "Your my friend" isn't that nice?"> 如您所见,最终结果并不连贯。现在的值是Hello I said 有一堆不合适的文字,比另一个字符串,不好。

如何将未知或可能不安全的字符串安全地输入到这些 HTML 属性中?

【问题讨论】:

  • 关于这个话题有几个现有的问题。此外,根据人类语言的规则,使用适当的标点符号几乎总是可以避免这个问题,例如"Hello I said “Your my friend” isn’t that nice?".

标签: html html-escape-characters


【解决方案1】:

使用HTML entities

<input type="text" value="Hello I said &quot;Your my friend&quot; isn't that nice?">

【讨论】:

    【解决方案2】:

    有多种解决方案,您可以选择自己喜欢的一种:

    破解: 1.你可以简单地使用'字符作为外引号并安全地使用"字符作为里面的文本。&lt;input type="text" value='this "should work"' name="something"&gt;

    正确方法: 2.根据HTML字符引用HERE对字符进行编码

    "&lt;input type="text" value="this &amp;quot;should work&amp;quot;" name="something"&gt;或&lt;input type="text" value="this &amp;#34;should work&amp;#34;" name="something"&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 2014-01-20
      • 2013-09-11
      • 1970-01-01
      • 2010-12-08
      • 2015-06-23
      相关资源
      最近更新 更多