我如何在HTML中转义一个'(单引号)?
这就是我试图使用它的地方:
<input type='text' id='abc' value='hel'lo'>
上述代码的结果是在文本框中填充“hel”。我试图用\'替换',但这是我得到的。
<input type='text' id='abc' value='hel\'lo'>
上述代码的结果是在文本框中填充“hel”。
如何成功地转义单引号?
我如何在HTML中转义一个'(单引号)?
这就是我试图使用它的地方:
<input type='text' id='abc' value='hel'lo'>
上述代码的结果是在文本框中填充“hel”。我试图用\'替换',但这是我得到的。
<input type='text' id='abc' value='hel\'lo'>
上述代码的结果是在文本框中填充“hel”。
如何成功地转义单引号?
当前回答
使用javascript内置函数escape和unescape
例如
var escapedData = escape("hel'lo");
output = "%27hel%27lo%27" which can be used in the attribute.
again to read the value from the attr
var unescapedData = unescape("%27hel%27lo%27")
output = "'hel'lo'"
如果你有大量的json stringify数据要在属性中使用,这将是有帮助的
其他回答
将其表示为文本实体(ASCII 39):
<input type='text' id='abc' value='hel'lo'>
如果出于某种原因,你不能转义撇号字符,你不能将它更改为HTML实体(就像我在特定Vue.js属性的情况下),你可以使用replace将它从UTF-8字符集更改为不同的撇号字符,例如:
ʼ - U+02BC
’ - U+2019
由于处于HTML上下文中,您需要使用HTML来表示该字符。对于HTML,你需要使用数字字符引用'(& # x27;十六进制):
<input type='text' id='abc' value='hel'lo'>
你可以使用'(这在IE中是不确定的)或'(这应该在任何地方都适用)。要获得全面的列表,请参阅W3C HTML5命名字符引用或WebPlatform.org上的HTML实体表。
你可以使用HTML实体:
& # 39;“ & # 34;为“ ...
要了解更多信息,可以查看HTML中的字符实体引用。