Stringhe ed entità

7 settembre 2010 at 14:23 Lascia un commento

Non c’è limite alle stranezze di javascript:

<a id="band-16">Guns &#038; Roses</a>
<script>
alert(document.getElementById("band-16").innerHTML);
// -> Guns &amp; Roses
alert(document.getElementById("band-16").firstChild.nodeValue);
// -> Guns & Roses
</script>

Come potete vedere, ho usato l’entità in formato decimale, ma se provo ad accedere al testo tramite JavaScript essa viene convertita! A volte torna utile, ma cosa potrei fare nel caso in cui mi servisse recuperare esattamente ciò che ho scritto? Per fortuna esiste il tag XMP:

<xmp id="xmp-16">Guns &#38; Roses</xmp>
<script>
alert(document.getElementById("xmp-16").innerHTML);
// -> Guns &#38; Roses
alert(document.getElementById("xmp-16").firstChild.nodeValue);
// -> Guns &#38; Roses
</script>

Entrata archiviata sotto: JavaScript. Tags: .

Attivare il metodo di un’istanza in maniera anonima Fix per Lightwindow

Lascia un Commento

Fill in your details below or click an icon to log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Log Out / Modifica )

Foto Twitter

You are commenting using your Twitter account. Log Out / Modifica )

Foto di Facebook

You are commenting using your Facebook account. Log Out / Modifica )

Connecting to %s

Trackback this post  |  Iscriviti ai commenti via RSS Feed


Mephit

Mephit logo
PHP project for d20 players www.mephit.it

Categorie

JavaScript String .replace

Archivi


Follow

Get every new post delivered to your Inbox.