2014-12-06 9 views
6

Gli esempi di seguito mostrano la domanda:Perché l'append di jQuery ha un output incoerente per i ritorni a capo?

$("#ex1").append("\r"); //This one works as expected 
 
$("#ex2").append("\n"); //This also works as expected 
 
$("#ex3").append("\r\n"); //This also works as expected 
 
$("#ex4").append("\r <el></el>"); //This replaces \r with \n 
 
$("#ex5").append("\r\n <el></el>"); //This removes \r completely 
 

 
$("div").on("click", function() { 
 
    alert(JSON.stringify(this.innerHTML)); 
 
});
<!-- Note: this also works in the JQuery 2.0 branch --> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
Click the divs below to see the result. 
 
<div id="ex1">R: </div> 
 
<div id="ex2">N: </div> 
 
<div id="ex3">RN: </div> 
 
<div id="ex4">R + El: </div> 
 
<div id="ex5">RN + El: </div>

Perché accodamento di jQuery hanno uscita incoerente per ritorni a capo?

Usando il DOM direttamente, aggiungendo nodi di testo e tale, il \r viene mantenuta (almeno su Chrome, su Linux):

document.getElementById("ex1").appendChild(document.createTextNode("\r")); 
 

 
document.getElementById("ex2").appendChild(document.createTextNode("\n")); 
 

 
document.getElementById("ex3").appendChild(document.createTextNode("\r\n")); 
 

 
document.getElementById("ex4").appendChild(document.createTextNode("\r ")); 
 
document.getElementById("ex4").appendChild(document.createElement('el')); 
 

 
document.getElementById("ex5").appendChild(document.createTextNode("\r\n ")); 
 
document.getElementById("ex5").appendChild(document.createElement('el')); 
 

 
document.addEventListener("click", function(e) { 
 
    if (/^ex\d$/.test(e.target.id)) { 
 
     alert(JSON.stringify(e.target.innerHTML)); 
 
    } 
 
}, false);
<!-- Note: this also works in the JQuery 2.0 branch --> 
 
Click the divs below to see the result. 
 
<div id="ex1">R: </div> 
 
<div id="ex2">N: </div> 
 
<div id="ex3">RN: </div> 
 
<div id="ex4">R + El: </div> 
 
<div id="ex5">RN + El: </div>

+4

Stai vedendo cosa fa il browser - non ha nulla con jQuery davvero. – Pointy

+0

@Pointy: Questo non sembra essere vero. Converti questi ultimi due in chiamate DOM raw aggiungendo un nodo di testo seguito da un elemento, e '\ r' è conservato (su Chrome su Linux), ma non se lo fai attraverso jQuery. –

+1

@ T.J.Crowder ah bene lo "sniffing" che jQuery fa per decidere cosa viene aggiunto può avere qualcosa a che fare con esso, probabilmente. – Pointy

risposta

0

http://jsfiddle.net/19gc7pjt/3/

Non è Il problema di jQuery.

Penso che sia un problema relativo al browser javascript.

(uso IE '\ r', gli altri usano '\ n'. Ho provato su Chrome btw ...)

potete vedere esattamente lo stesso risultato in finestre di avviso. '\ r' viene ignorato.

$("#ex1").append("\r"); //This one works as expected 
$("#ex2").append("\n"); //This also works as expected 
$("#ex3").append("\r\n"); //This also works as expected 
$("#ex4").append("\r <el></el>"); //This replaces \r with \n 
$("#ex5").append("\r\n <el></el>"); //This removes \r completely 

var text4 = "\r <el></el>"; 
var text5 = "\r\n <el></el>"; 

alert(text4); //check this 
alert(text5); //and this 

$("div").on("click", function() { 
    alert(JSON.stringify(this.innerHTML)); 
}); 
+0

Se si chiama 'alert' senza usare' JSON.stringify', il ritorno a capo viene trattato come un carattere di spazio bianco. È ancora lì, solo non visibile. –

Problemi correlati