Aggiornamento 2018
Poiché questa è una risposta piuttosto popolare, ho deciso di aggiornarla e abbellirla un po 'aggiungendo il selettore textnode a jQuery come plugin.
Nello snippet qui sotto puoi vedere che definisco una nuova funzione jQuery che ottiene tutti (e solo) i textNodes. È possibile concatenare questa funzione anche con, ad esempio, la first()
funzione. Faccio un taglio sul nodo di testo e controllo se non è vuoto dopo il taglio perché anche spazi, tabulazioni, nuove linee, ecc. Sono riconosciuti come nodi di testo. Se hai bisogno anche di quei nodi, rimuovilo semplicemente dall'istruzione if nella funzione jQuery.
Ho aggiunto un esempio su come sostituire il primo nodo di testo e su come sostituire tutti i nodi di testo.
Questo approccio rende più facile leggere il codice e più facile utilizzarlo più volte e con scopi diversi.
L' aggiornamento 2017 (adrach) dovrebbe comunque funzionare anche se lo preferisci.
Come estensione jQuery
//Add a jQuery extension so it can be used on any jQuery object
jQuery.fn.textNodes = function() {
return this.contents().filter(function() {
return (this.nodeType === Node.TEXT_NODE && this.nodeValue.trim() !== "");
});
}
//Use the jQuery extension
$(document).ready(function(){
$('#replaceAll').on('click', () => {
$('#testSubject').textNodes().replaceWith('Replaced');
});
$('#replaceFirst').on('click', () => {
$('#testSubject').textNodes().first().replaceWith('Replaced First');
});
});
p {
margin: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="testSubject">
**text to change**
<p>text that should not change</p>
<p>text that should not change</p>
**also text to change**
<p>text that should not change</p>
<p>text that should not change</p>
**last text to change**
</div>
<button id="replaceFirst">Replace First</button>
<button id="replaceAll">Replace All</button>
Eqivilant Javascript (ES)
//Add a new function to the HTMLElement object so it cna be used on any HTMLElement
HTMLElement.prototype.textNodes = function() {
return [...this.childNodes].filter((node) => {
return (node.nodeType === Node.TEXT_NODE && node.nodeValue.trim() !== "");
});
}
//Use the new HTMLElement function
document.addEventListener('DOMContentLoaded', () => {
document.querySelector('#replaceAll').addEventListener('click', () => {
document.querySelector('#testSubject').textNodes().forEach((node) => {
node.textContent = 'Replaced';
});
});
document.querySelector('#replaceFirst').addEventListener('click', function() {
document.querySelector('#testSubject').textNodes()[0].textContent = 'Replaced First';
});
});
p {
margin: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="testSubject">
**text to change**
<p>text that should not change</p>
<p>text that should not change</p>
**also text to change**
<p>text that should not change</p>
<p>text that should not change</p>
**last text to change**
</div>
<button id="replaceFirst">Replace First</button>
<button id="replaceAll">Replace All</button>
Aggiornamento 2017 (adrach):
Sembra che diverse cose siano cambiate da quando è stato pubblicato. Ecco una versione aggiornata
$("div").contents().filter(function(){ return this.nodeType == 3; }).first().replaceWith("change text");
Risposta originale (non funziona per le versioni attuali)
$("div").contents().filter(function(){ return this.nodeType == 3; })
.filter(':first').text("change text");
Fonte: http://api.jquery.com/contents/