2010-04-24 5 views
0

Quelle est la règle derrière pour diviser ce code HTML dans var tip?Quelle est la règle derrière pour diviser ce code HTML dans var tip?

var tip = "<p class='adobe-reader-download'>Most computers 
    will open PDF documents automatically, but you may need to download 
    <a title='Link to Adobe website-opens in a new window'"; 
    tip += " href='http://www.adobe.com/products/acrobat/readstep2.html' 
target='_blank'>Adobe Reader</a>. 
    </p>"; 

pourquoi cela ne peut pas être

var tip = "<p class='adobe-reader-download'>Most computers will 
    open PDF documents automatically, but you may need to download 
    <a title='Link to Adobe website-opens in a new window' 
href='http://www.adobe.com/products/acrobat/readstep2.html' target='_blank'> 
    Adobe Reader</a>.</p>"; 

et comment diviser en HTML est plus que cela?

Répondre

1

Vous devez mettre un \ à la fin de la ligne pour indiquer à Javascript que la chaîne s'étend sur la ligne suivante.

var tip = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \ 
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb \ 
ccccccccccccccccccccccccccccccccccccccccccc"; 

Il ne se fait pas très souvent mais ... vous trouverez peut-être ce qui suit plus lisible:

var tip = "<p class='adobe-reader-download'>"; 

tip += "Most computers will open PDF documents automatically, "; 
tip += "but you may need to download "; 
tip += "<a title='Link to Adobe website-opens in a new window' "; 
tip += "href='http://www.adobe.com/products/acrobat/readstep2.html' target='_blank'>"; 
tip += "Adobe Reader</a>.</p>"; 

etc.

+0

est-il un outil qui peut convertir tout code 'de html' dans ce Le format 'Tip de Var'? –

+0

Quelle est la différence entre 'document.write' et' var tip'? –

+0

Pas que je sache. 'document.write' analysera la chaîne en HTML et écrira le résultat sur la page (si' document.write' est appelé après le chargement du document, il remplacera le contenu actuel), cependant 'var' crée simplement une variable Javascript , qui est une chaîne (note, chaîne, pas l'élément DOM). – Matt

Questions connexes