0

例如给定一个表单输入字段给<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 属性中?

4

2 回答 2

3

使用HTML 实体

<input type="text" value="Hello I said &quot;Your my friend&quot; isn't that nice?">
于 2013-04-14T19:40:41.753 回答
0

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

技巧: 1. 您可以简单地使用 ' 字符作为外部引号,并安全地使用 " 字符作为内部文本。<input type="text" value='this "should work"' name="something">

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

"<input type="text" value="this &quot;should work&quot;" name="something"><input type="text" value="this &#34;should work&#34;" name="something">

于 2013-04-14T20:04:46.630 回答