如何在标题属性中转义双引号

时间:2022-09-15 15:35:42

I am trying to use a string that contains double quotes in the title attribute of an anchor. So far I tried these:

我正在尝试使用一个字符串,该字符串在锚的title属性中包含双引号。到目前为止,我尝试了以下方法:

<a href=".." title="Some \"text\"">Some text</a>
<!-- title looks like `Some \` --!>

and

<a href=".." title="Some &quot;text&quot;">Some text</a>
<!-- title looks like `Some ` --!>

Please note that using single quotes is not an option.

请注意,使用单引号不是一个选项。

8 个解决方案

#1


227  

This variant -

这变种-

<a href=".." title="Some &quot;text&quot;">Some text</a>

Is correct and it works as expected - you see normal quotes in rendered page.

是正确的,并且它可以按预期工作—您可以在呈现的页面中看到正常的引号。

#2


19  

EDIT: Link appears to be dead, so here's a snippet of the escape characters taken from a cached page on archive.org:

编辑:链接似乎是死的,所以这里有一个转义字符的片段,从archive.org上缓存的页面中获取:

&#060   |   less than sign  <       
&#064   |   at sign @       
&#093   |   right bracket   ]       
&#123   |   left curly brace    {       
&#125   |   right curly brace   }       
&#133   |   ellipsis    …       
&#135   |   double dagger   ‡       
&#146   |   right single quote  ’       
&#148   |   right double quote  ”       
&#150   |   short dash  –       
&#153   |   trademark   ™       
&#162   |   cent sign   ¢       
&#165   |   yen sign    ¥       
&#169   |   copyright sign  ©       
&#172   |   logical not sign    ¬       
&#176   |   degree sign °       
&#178   |   superscript 2   ²       
&#185   |   superscript 1   ¹       
&#188   |   fraction 1/4    ¼       
&#190   |   fraction 3/4    ¾       
&#247   |   division sign   ÷       
&#8221  |   right double quote  ”       
&#062   |   greater than sign   >   
&#091   |   left bracket    [   
&#096   |   back apostrophe `   
&#124   |   vertical bar    |   
&#126   |   tilde   ~   
&#134   |   dagger  †   
&#145   |   left single quote   ‘       
&#147   |   left double quote   “   
&#149   |   bullet  •   
&#151   |   longer dash —   
&#161   |   inverted excallamation point    ¡   
&#163   |   pound sign  £   
&#166   |   broken vertical bar ¦   
&#171   |   double left than sign   «   
&#174   |   registered trademark sign   ®   
&#177   |   plus or minus sign  ±   
&#179   |   superscript 3   ³   
&#187   |   double greather than sign   »   
&#189   |   fraction 1/2    ½   
&#191   |   inverted question mark  ¿   
&#8220  |   left double quote   “   
&#8212  |   dash    —   

/EDIT

/编辑

Give this a shot

给这一枪

HTML Escape character list.

HTML转义字符列表。

It's a great reference for all of these characters.

这对所有这些角色都是很好的参考。

#3


6  

Does &#34; instead of &quot; work?

& # 34;而不是“;工作吗?

#4


3  

Using &quot; is the way to do it, I tried you second code snippet and it works on both Firefox and IE.

使用“;就是这么做的,我试过你第二段代码,它在Firefox和IE上都能运行。

#5


2  

It may work with any character from the HTML Escape character list, but I had the same problem with a Java project. I used StringEscapeUtils.escapeHTML("Testing \" <br> <p>") and the title was <a href=".." title="Test&quot; &lt;br&gt; &lt;p&gt;">Testing</a>.

它可能与HTML转义字符列表中的任何字符一起工作,但是我在Java项目中遇到了同样的问题。我使用了StringEscapeUtils.escapeHTML("测试\"

"),标题为 < / >。

It only worked for me when I changed the StringEscapeUtils to StringEscapeUtils.escapeJavascript("Testing \" <br> <p>") and it worked in every browser.

它只在我将StringEscapeUtils改成stringescapeutills . escapejavascript(“Testing \”

”)时对我有效,并且在每个浏览器中都可以使用。

#6


1  

There is at least one situation where using single quotes will not work and that is if you are creating the markup "on the fly" from Javascript. You use single quotes to contain the string and then any property in the markup can have double quotes for its value.

至少有一种情况是单引号不起作用,那就是如果您正在从Javascript创建标记“on the fly”。您可以使用单引号来包含字符串,然后标记中的任何属性都可以有双引号。

#7


0  

Perhaps you can use JavaScript to solve your cross-browser problem. It uses a different escape mechanism, one with which you're obviously already familiar:

也许您可以使用JavaScript来解决跨浏览器的问题。它使用一种不同的逃避机制,显然你已经很熟悉了:

(reference-to-the-tag).title = "Some \"text\"";

It doesn't strictly separate the functions of HTML, JS and CSS the way folks want you to nowadays, but whom do you need to make happy? Your users or techies you don't know?

它并没有严格地将HTML、JS和CSS的功能与现在的人们想要的方式分开,但是你需要让谁开心呢?你不认识的用户或技术人员?

#8


-1  

You can use this PHP code to list special characters...

您可以使用此PHP代码来列出特殊字符…

<table border="1"><?php for($i=33;$i<9000;$i++)echo "<tr><td>&#38;#$i;<td>&#".$i.";"; ?></table>

#1


227  

This variant -

这变种-

<a href=".." title="Some &quot;text&quot;">Some text</a>

Is correct and it works as expected - you see normal quotes in rendered page.

是正确的,并且它可以按预期工作—您可以在呈现的页面中看到正常的引号。

#2


19  

EDIT: Link appears to be dead, so here's a snippet of the escape characters taken from a cached page on archive.org:

编辑:链接似乎是死的,所以这里有一个转义字符的片段,从archive.org上缓存的页面中获取:

&#060   |   less than sign  <       
&#064   |   at sign @       
&#093   |   right bracket   ]       
&#123   |   left curly brace    {       
&#125   |   right curly brace   }       
&#133   |   ellipsis    …       
&#135   |   double dagger   ‡       
&#146   |   right single quote  ’       
&#148   |   right double quote  ”       
&#150   |   short dash  –       
&#153   |   trademark   ™       
&#162   |   cent sign   ¢       
&#165   |   yen sign    ¥       
&#169   |   copyright sign  ©       
&#172   |   logical not sign    ¬       
&#176   |   degree sign °       
&#178   |   superscript 2   ²       
&#185   |   superscript 1   ¹       
&#188   |   fraction 1/4    ¼       
&#190   |   fraction 3/4    ¾       
&#247   |   division sign   ÷       
&#8221  |   right double quote  ”       
&#062   |   greater than sign   >   
&#091   |   left bracket    [   
&#096   |   back apostrophe `   
&#124   |   vertical bar    |   
&#126   |   tilde   ~   
&#134   |   dagger  †   
&#145   |   left single quote   ‘       
&#147   |   left double quote   “   
&#149   |   bullet  •   
&#151   |   longer dash —   
&#161   |   inverted excallamation point    ¡   
&#163   |   pound sign  £   
&#166   |   broken vertical bar ¦   
&#171   |   double left than sign   «   
&#174   |   registered trademark sign   ®   
&#177   |   plus or minus sign  ±   
&#179   |   superscript 3   ³   
&#187   |   double greather than sign   »   
&#189   |   fraction 1/2    ½   
&#191   |   inverted question mark  ¿   
&#8220  |   left double quote   “   
&#8212  |   dash    —   

/EDIT

/编辑

Give this a shot

给这一枪

HTML Escape character list.

HTML转义字符列表。

It's a great reference for all of these characters.

这对所有这些角色都是很好的参考。

#3


6  

Does &#34; instead of &quot; work?

& # 34;而不是“;工作吗?

#4


3  

Using &quot; is the way to do it, I tried you second code snippet and it works on both Firefox and IE.

使用“;就是这么做的,我试过你第二段代码,它在Firefox和IE上都能运行。

#5


2  

It may work with any character from the HTML Escape character list, but I had the same problem with a Java project. I used StringEscapeUtils.escapeHTML("Testing \" <br> <p>") and the title was <a href=".." title="Test&quot; &lt;br&gt; &lt;p&gt;">Testing</a>.

它可能与HTML转义字符列表中的任何字符一起工作,但是我在Java项目中遇到了同样的问题。我使用了StringEscapeUtils.escapeHTML("测试\"

"),标题为 < / >。

It only worked for me when I changed the StringEscapeUtils to StringEscapeUtils.escapeJavascript("Testing \" <br> <p>") and it worked in every browser.

它只在我将StringEscapeUtils改成stringescapeutills . escapejavascript(“Testing \”

”)时对我有效,并且在每个浏览器中都可以使用。

#6


1  

There is at least one situation where using single quotes will not work and that is if you are creating the markup "on the fly" from Javascript. You use single quotes to contain the string and then any property in the markup can have double quotes for its value.

至少有一种情况是单引号不起作用,那就是如果您正在从Javascript创建标记“on the fly”。您可以使用单引号来包含字符串,然后标记中的任何属性都可以有双引号。

#7


0  

Perhaps you can use JavaScript to solve your cross-browser problem. It uses a different escape mechanism, one with which you're obviously already familiar:

也许您可以使用JavaScript来解决跨浏览器的问题。它使用一种不同的逃避机制,显然你已经很熟悉了:

(reference-to-the-tag).title = "Some \"text\"";

It doesn't strictly separate the functions of HTML, JS and CSS the way folks want you to nowadays, but whom do you need to make happy? Your users or techies you don't know?

它并没有严格地将HTML、JS和CSS的功能与现在的人们想要的方式分开,但是你需要让谁开心呢?你不认识的用户或技术人员?

#8


-1  

You can use this PHP code to list special characters...

您可以使用此PHP代码来列出特殊字符…

<table border="1"><?php for($i=33;$i<9000;$i++)echo "<tr><td>&#38;#$i;<td>&#".$i.";"; ?></table>