Perbarui 2018
Karena ini adalah jawaban yang cukup populer, saya memutuskan untuk memperbarui dan mempercantiknya sedikit dengan menambahkan pemilih textnode ke jQuery sebagai plugin.
Dalam potongan di bawah ini Anda dapat melihat bahwa saya mendefinisikan fungsi jQuery baru yang mendapatkan semua (dan hanya) textNodes. Anda dapat menghubungkan first()fungsi ini juga dengan misalnya fungsi tersebut. Saya melakukan trim pada node teks dan memeriksa apakah tidak kosong setelah trim karena spasi, tab, baris baru, dll. Juga dikenali sebagai node teks. Jika Anda membutuhkan node itu juga, cukup hapus itu dari pernyataan if di fungsi jQuery.
Saya menambahkan contoh bagaimana mengganti simpul teks pertama dan bagaimana mengganti semua simpul teks.
Pendekatan ini membuatnya lebih mudah untuk membaca kode dan lebih mudah untuk digunakan berkali-kali dan dengan tujuan yang berbeda.
The Perbarui 2017 (adrach) harus tetap bekerja dengan baik jika Anda lebih suka itu.
Sebagai ekstensi 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>
Perbarui 2017 (adrach):
Sepertinya ada beberapa hal yang berubah sejak ini diposting. Ini adalah versi yang diperbarui
$("div").contents().filter(function(){ return this.nodeType == 3; }).first().replaceWith("change text");
Jawaban asli (Tidak berfungsi untuk versi saat ini)
$("div").contents().filter(function(){ return this.nodeType == 3; })
.filter(':first').text("change text");
Sumber: http://api.jquery.com/contents/