I am trying to use a string that contains double quotes in the title attribute of an anchor. So far I tried these:
<a href=".." title="Some \"text\"">Some text</a>
<!-- title looks like `Some \` --!>
and
<a href=".." title="Some "text"">Some text</a>
<!-- title looks like `Some ` --!>
Please note that using single quotes is not an option.
This variant -
<a href=".." title="Some "text"">Some text</a>
Is correct and it works as expected - you see normal quotes in rendered page.
EDIT: Link appears to be dead, so here's a snippet of the escape characters taken from a cached page on archive.org:
< | less than sign <
@ | at sign @
] | right bracket ]
{ | left curly brace {
} | right curly brace }
… | ellipsis …
‡ | double dagger ‡
’ | right single quote ’
” | right double quote ”
– | short dash –
™ | trademark ™
¢ | cent sign ¢
¥ | yen sign ¥
© | copyright sign ©
¬ | logical not sign ¬
° | degree sign °
² | superscript 2 ²
¹ | superscript 1 ¹
¼ | fraction 1/4 ¼
¾ | fraction 3/4 ¾
÷ | division sign ÷
” | right double quote ”
> | greater than sign >
[ | left bracket [
` | back apostrophe `
| | vertical bar |
~ | tilde ~
† | dagger †
‘ | left single quote ‘
“ | left double quote “
• | bullet •
— | longer dash —
¡ | inverted excallamation point ¡
£ | pound sign £
¦ | broken vertical bar ¦
« | double left than sign «
® | registered trademark sign ®
± | plus or minus sign ±
³ | superscript 3 ³
» | double greather than sign »
½ | fraction 1/2 ½
¿ | inverted question mark ¿
“ | left double quote “
— | dash —
/EDIT
Give this a shot
It's a great reference for all of these characters.
The escape code " can also be used instead of ".
Using " is the way to do it, I tried you second code snippet and it works on both Firefox and IE.
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" <br> <p>">Testing</a>.
It only worked for me when I changed the StringEscapeUtils to StringEscapeUtils.escapeJavascript("Testing \" <br> <p>") and it worked in every browser.
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.
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:
(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?
You can use this PHP code to list special characters...
<table border="1"><?php for($i=33;$i<9000;$i++)echo "<tr><td>&#$i;<td>&#".$i.";"; ?></table>
来源:https://stackoverflow.com/questions/3752769/how-to-escape-double-quotes-in-title-attribute