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>
Stai vedendo cosa fa il browser - non ha nulla con jQuery davvero. – Pointy
@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. –
@ 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