2010-06-04 7 views
5

Ho trascorso due giorni su questo, quindi sarò scoraggiato se c'è una risposta semplice. Sto provando a mettere un tag span attorno ad ogni lettera in un div, lasciando intatto il resto dei tag.Utilizzo di jQuery: come si fa a racchiudere ogni lettera in un tag?

<div id="div"> 
    <p> 
     Of course some of the <strong>text is in other tags</strong> and <strong>some 
     is in <em>nested tags</em>, etc.</strong> 
    </p> 
</div> 

Mi avvicino molto, ma alla fine qualcosa mi incalza sempre.

+0

sono le lettere nei tag bambino supposti anche per ottenere un tag circostante ? –

+5

Perché vuoi farlo? – aviraldg

+0

Questo non è a conoscenza dei tag nidificati, ma potrebbe dare alcuni groudnwork: http://stackoverflow.com/questions/1966476/javascript-process-each-letter-of-text –

risposta

2

Ho capito! Questa potrebbe non essere la soluzione ottimale, ma funziona! Si noti inoltre che a causa dei tag extra, lo spazio bianco potrebbe essere incasinato. Questo include anche le schede ma è anche facile da correggere.

function wrap(target) { 
    var newtarget = $("<div></div>"); 
    nodes = target.contents().clone(); // the clone is critical! 
    nodes.each(function() { 
     if (this.nodeType == 3) { // text 
      var newhtml = ""; 
      var text = this.wholeText; // maybe "textContent" is better? 
      for (var i=0; i < text.length; i++) { 
       if (text[i] == ' ') newhtml += " "; 
       else newhtml += "<span>" + text[i] + "</span>"; 
      } 
      newtarget.append($(newhtml)); 
     } 
     else { // recursion FTW! 
      $(this).html(wrap($(this))); 
      newtarget.append($(this)); 
     } 
    }); 
    return newtarget.html(); 
} 

Usage:

$("#div").html(wrap($("#div"))); 
+0

Questo non sembra funzionare per me - posso vedere che crea correttamente la stringa newhtml in un debugger, ma '$ (this) .html (newhtml)' non comporta alcuna modifica all'output .. – Ryley

+0

L'ho risolto! Forse più persone possono trovare modi per ottimizzare? – Tesserex

+0

L'ho anche testato su una copia salvata della pagina jQuery API. Ci è voluto un secondo, ma ha funzionato, l'aspetto della pagina non è cambiato, tutto è intatto. Sembra funzionare. – Tesserex

1
function init(target) { 
var newtarget = $('<div></div>'); 
nodes = target.contents().clone(); // the clone is critical! 
nodes.each(function(i,v) { 
    if (v.nodeType == 3) { // text 
     if($.trim($(this).text()).length>0){ 
      var letters=$(this).text().split(''); 
      for (var j = 0; j < letters.length; j++) { 
       newtarget.append('<span class="letter">'+letters[j]+'</span>') 
      } 
     } 
    } 
    else { // recursion FTW! 
     newtarget.append($(this)); 
     $(this).html(init($(this))); 
    } 
}); 
return newtarget.html(); 
} 

Questo funziona abbastanza bene. Tuttavia, vale a dire (7 in ogni caso), elimina tutti gli spazi. Inoltre, dovrei rimuovere newtarget dal dom alla fine della funzione? E il clone? Dovrebbe essere rimosso?

Problemi correlati