2018 Güncellemesi
Bu oldukça popüler bir cevap olduğundan, textnode seçiciyi bir eklenti olarak jQuery'ye ekleyerek biraz güncellemeye ve güzelleştirmeye karar verdim.
Aşağıdaki kod parçacığında, tüm (ve yalnızca) textNode'ları alan yeni bir jQuery işlevi tanımladığımı görebilirsiniz. Bu işlevi, örneğin işlevle zincirleyebilirsiniz first(). Metin düğümünde bir kırpma yapıyorum ve kırpmadan sonra boş olup olmadığını kontrol ediyorum çünkü boşluklar, sekmeler, yeni satırlar vb. De metin düğümleri olarak tanınır. Bu düğümlere de ihtiyacınız varsa, jQuery işlevindeki if ifadesinden basitçe kaldırın.
İlk metin düğümünün nasıl değiştirileceğine ve tüm metin düğümlerinin nasıl değiştirileceğine bir örnek ekledim.
Bu yaklaşım, kodu okumayı ve birden çok kez ve farklı amaçlarla kullanmayı kolaylaştırır.
Bunu tercih ederseniz, 2017 Güncellemesi (adrach) yine de iyi çalışacaktır.
JQuery uzantısı olarak
//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>
Javascript (ES) eqivilant
//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>
2017 Güncellemesi (adrach):
Bu yayınlandıktan sonra birkaç şey değişmiş gibi görünüyor. İşte güncellenmiş bir versiyon
$("div").contents().filter(function(){ return this.nodeType == 3; }).first().replaceWith("change text");
Orijinal cevap (Mevcut sürümler için çalışmıyor)
$("div").contents().filter(function(){ return this.nodeType == 3; })
.filter(':first').text("change text");
Kaynak: http://api.jquery.com/contents/