อัปเดต 2018
เนื่องจากนี่เป็นคำตอบที่ได้รับความนิยมมากฉันจึงตัดสินใจอัปเดตและตกแต่งมันเล็กน้อยโดยการเพิ่มตัวเลือกโหนดข้อความลงใน jQuery เป็นปลั๊กอิน
ในตัวอย่างด้านล่างคุณจะเห็นว่าฉันกำหนดฟังก์ชัน jQuery ใหม่ที่รับ textNodes ทั้งหมด (และเท่านั้น) คุณสามารถเชื่อมต่อฟังก์ชันนี้ได้เช่นกันกับตัวอย่างเช่นfirst()
ฟังก์ชัน ฉันทำการตัดแต่งบนโหนดข้อความและตรวจสอบว่ามันไม่ว่างเปล่าหลังจากการตัดแต่งหรือไม่เนื่องจากช่องว่างแท็บบรรทัดใหม่ ฯลฯ ได้รับการยอมรับว่าเป็นโหนดข้อความ หากคุณต้องการโหนดเหล่านั้นด้วยให้ลบออกจากคำสั่ง if ในฟังก์ชัน jQuery
ฉันได้เพิ่มตัวอย่างวิธีการแทนที่โหนดข้อความแรกและวิธีแทนที่โหนดข้อความทั้งหมด
วิธีนี้ช่วยให้อ่านโค้ดได้ง่ายขึ้นและใช้หลายครั้งได้ง่ายขึ้นและมีวัตถุประสงค์ที่แตกต่างกัน
การอัปเดต 2017 (adrach)ควรใช้งานได้เช่นกันหากคุณต้องการ
เป็นส่วนขยาย 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>
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 (adrach):
ดูเหมือนว่ามีหลายอย่างเปลี่ยนไปตั้งแต่โพสต์นี้ นี่คือเวอร์ชันอัปเดต
$("div").contents().filter(function(){ return this.nodeType == 3; }).first().replaceWith("change text");
คำตอบเดิม (ใช้ไม่ได้กับเวอร์ชันปัจจุบัน)
$("div").contents().filter(function(){ return this.nodeType == 3; })
.filter(':first').text("change text");
ที่มา: http://api.jquery.com/contents/