अपडेट 2018
चूँकि यह एक बहुत लोकप्रिय उत्तर है, मैंने एक प्लगइन के रूप में jQuery के टेक्स्टनोडेक्टर चयनकर्ता को जोड़कर इसे थोड़ा अद्यतन और सुशोभित करने का फैसला किया।
नीचे दिए गए स्निपेट में आप देख सकते हैं कि मैं एक नया jQuery फ़ंक्शन परिभाषित करता हूं जो सभी को प्राप्त करता है (और केवल) टेक्स्टनोड्स। आप इस फ़ंक्शन की श्रृंखला के साथ-साथ first()फ़ंक्शन के उदाहरण के लिए भी कर सकते हैं। मैं टेक्स्ट नोड पर एक ट्रिम करता हूं और यह जांचता हूं कि क्या यह ट्रिम के बाद खाली नहीं है क्योंकि रिक्त स्थान, टैब, नई लाइनें आदि को टेक्स्ट नोड के रूप में भी पहचाना जाता है। यदि आपको उन नोड्स की भी आवश्यकता है, तो 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>
जावास्क्रिप्ट (ईएस) 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 (पालन):
ऐसा लगता है कि यह पोस्ट किए जाने के बाद से कई चीजें बदल गई हैं। यहाँ एक अद्यतन संस्करण है
$("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/