Cómo escapar de comillas dobles en el atributo title


Estoy tratando de usar una cadena que contiene comillas dobles en el atributo title de un ancla. Hasta ahora he intentado estos:

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

Y

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

Tenga en cuenta que usar comillas simples es no una opción.

Author: Mark Schultheiss, 2010-09-20

8 answers

Esta variante -

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

Es correcto y funciona como se espera - se ven comillas normales en la página renderizada.

 235
Author: Māris Kiseļovs,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-20 15:13:17

EDITAR: El enlace parece estar muerto, así que aquí hay un fragmento de los caracteres de escape tomados de una página en caché en 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    —   

/ EDITAR

Dale una oportunidad

Lista de caracteres de escape HTML.

Es una gran referencia para todos estos personajes.

 20
Author: Dave,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-21 13:42:16

El código de escape &#34; también se puede usar en lugar de &quot;.

 7
Author: fredley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-30 16:40:24

Usando &quot; es la manera de hacerlo, probé el segundo fragmento de código y funciona tanto en Firefox como en IE.

 3
Author: TheCee,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-20 15:25:27

Puede funcionar con cualquier carácter de la lista de caracteres de escape HTML , pero tuve el mismo problema con un proyecto Java. Usé StringEscapeUtils.escapeHTML("Testing \" <br> <p>") y el título era <a href=".." title="Test&quot; &lt;br&gt; &lt;p&gt;">Testing</a>.

Solo funcionó para mí cuando cambié el StringEscapeUtils a StringEscapeUtils.escapeJavascript("Testing \" <br> <p>") y funcionó en todos los navegadores.

 2
Author: Rodrigo Horta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-14 13:52:14

Hay al menos una situación en la que el uso de comillas simples no funcionará y es si está creando el marcado "sobre la marcha" desde Javascript. Utiliza comillas simples para contener la cadena y luego cualquier propiedad en el marcado puede tener comillas dobles para su valor.

 1
Author: cvadillo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-17 17:26:33

Tal vez pueda usar JavaScript para resolver su problema entre navegadores. Utiliza un mecanismo de escape diferente, uno con el que obviamente ya estás familiarizado:

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

No separa estrictamente las funciones de HTML, JS y CSS de la manera que la gente quiere que hoy en día, pero ¿a quién necesitas para hacer feliz? ¿Tus usuarios o técnicos que no conoces?

 0
Author: WebManWalking,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-25 09:43:06

Puede usar este código PHP para listar caracteres especiales...

<table border="1"><?php for($i=33;$i<9000;$i++)echo "<tr><td>&#38;#$i;<td>&#".$i.";"; ?></table>
 -2
Author: user6000975,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-25 09:53:31