Html 如何在标题属性中转义双引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3752769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to escape double quotes in a title attribute
提问by harpax
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>
<!-- The title looks like `Some \` --!>
and
和
<a href=".." title="Some "text"">Some text</a>
<!-- The title looks like `Some ` --!>
Please note that using single quotes is notan option.
请注意,不能使用单引号。
回答by Māris Kise?ovs
This variant -
这个变种——
<a title="Some "text"">Hover me</a>
Is correct and it works as expected - you see normal quotes in rendered page.
正确并且按预期工作 - 您会在呈现的页面中看到正常的引号。
回答by Dave
Here's a snippet of the HTML escape characters taken from a cached page on archive.org:
这是从archive.org 上的缓存页面中提取的 HTML 转义字符的片段:
< | 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 2
¹ | superscript 1 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 exclamation point ?
£ | pound sign £
¦ | broken vertical bar |
« | double left than sign ?
® | registered trademark sign ?
± | plus or minus sign ±
³ | superscript 3 3
» | double greater-than sign ?
½ | fraction 1/2 ?
¿ | inverted question mark ?
“ | left double quote “
— | dash —
回答by fredley
The escape code "
can also be used instead of "
.
"
也可以使用转义码代替"
.
回答by TheCee
Using "
is the way to do it. I tried your second code snippet, and it works in both Firefox and Internet Explorer.
使用"
是这样做的方法。我尝试了你的第二个代码片段,它在 Firefox 和 Internet Explorer 中都有效。
回答by Rodrigo Horta
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>
.
它可能适用于HTML Escape character list 中的任何字符,但我在 Java 项目中遇到了同样的问题。我用过StringEscapeUtils.escapeHTML("Testing \" <br> <p>")
,标题是<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.
它仅在我将 StringEscapeUtils 更改为时才对我有用,StringEscapeUtils.escapeJavascript("Testing \" <br> <p>")
并且它在每个浏览器中都有效。
回答by cvadillo
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 “即时”创建标记。您使用单引号来包含字符串,然后标记中的任何属性都可以对其值使用双引号。
回答by WebManWalking
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, JavaScript, 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、JavaScript 和 CSS 的功能,但是你需要让谁开心?您不认识的用户或技术人员?
回答by user6000975
You can use this PHP code to list special characters...
您可以使用此 PHP 代码列出特殊字符...
<table border="1"><?php for($i=33;$i<9000;$i++)echo "<tr><td>&#$i;<td>&#".$i.";"; ?></table>