如何避免引用某句话?

时间:2022-09-15 16:14:16

How can I escape a ' (single quote) in JavaScript?

如何在JavaScript中转义“(单引号)”?

This is where I'm trying to use it:

这就是我要用到它的地方:

<input type='text' id='abc' value='hel'lo'>

The result for the above code is "hel" populated in the text box. I tried to replace ' with \', but this what I'm getting.

上述代码的结果是在文本框中填充“hel”。我试着用“\”代替,但这是我得到的。

<input type='text' id='abc' value='hel\'lo'>

The result for the above code is "hel\" populated in the text box.

上述代码的结果是在文本框中填充的“hel\”。

How can I successfully escape the single quotes?

如何成功地转义单引号?

6 个解决方案

#1


249  

You could use HTML entities:

您可以使用HTML实体:

  • &#39; for '
  • & # 39;“
  • &#34; for "
  • & # 34;为“
  • ...

For more, you can take a look at Character entity references in HTML.

更多信息,您可以查看HTML中的字符实体引用。

#2


46  

You can use &apos; (which is iffy in IE) or &#39; (which should work everywhere). For a comprehensive list, see the W3C HTML5 Named Character References or the HTML entities table on WebPlatform.org.

事情就让它可以使用,(在IE中是不确定的)或#39;(这应该无处不在)。有关全面的列表,请参阅WebPlatform.org上的W3C HTML5命名字符引用或HTML实体表。

#3


7  

As you’re in the context of HTML, you need to use HTML to represent that character. And for HTML you need to use a numeric character reference &#39; (&#x27; hexadecimal):

在HTML上下文中,您需要使用HTML来表示该字符。对于HTML,你需要使用数字字符引用'(& # x27;十六进制):

<input type='text' id='abc' value='hel&#39;lo'>

#4


5  

Represent it as text entity (ASCII 39):

将其表示为文本实体(ASCII 39):

<input type='text' id='abc' value='hel&#39;lo'>

#5


1  

Probably the easiest way:

可能最简单的方法:

<input type='text' id='abc' value="hel'lo">

#6


-2  

You could try using: &#145;

您可以尝试使用:‘

#1


249  

You could use HTML entities:

您可以使用HTML实体:

  • &#39; for '
  • & # 39;“
  • &#34; for "
  • & # 34;为“
  • ...

For more, you can take a look at Character entity references in HTML.

更多信息,您可以查看HTML中的字符实体引用。

#2


46  

You can use &apos; (which is iffy in IE) or &#39; (which should work everywhere). For a comprehensive list, see the W3C HTML5 Named Character References or the HTML entities table on WebPlatform.org.

事情就让它可以使用,(在IE中是不确定的)或#39;(这应该无处不在)。有关全面的列表,请参阅WebPlatform.org上的W3C HTML5命名字符引用或HTML实体表。

#3


7  

As you’re in the context of HTML, you need to use HTML to represent that character. And for HTML you need to use a numeric character reference &#39; (&#x27; hexadecimal):

在HTML上下文中,您需要使用HTML来表示该字符。对于HTML,你需要使用数字字符引用'(& # x27;十六进制):

<input type='text' id='abc' value='hel&#39;lo'>

#4


5  

Represent it as text entity (ASCII 39):

将其表示为文本实体(ASCII 39):

<input type='text' id='abc' value='hel&#39;lo'>

#5


1  

Probably the easiest way:

可能最简单的方法:

<input type='text' id='abc' value="hel'lo">

#6


-2  

You could try using: &#145;

您可以尝试使用:‘