निम्न कोड का उपयोग करके, मुझे INDEX_SIZE_ERR: इस Range.setStart लाइन पर DOM अपवाद 1 त्रुटि मिलती है। कोड पूरे पृष्ठ पर जाने के लिए है, खोज स्ट्रिंग के उदाहरण ढूंढें, और उसके बाद उस खोज स्ट्रिंग के सामने एक लिंक जोड़ें। उदाहरण के लिए, यदि यह स्ट्रिंग के 5 उदाहरण पाता है, तो अभी यह लिंक पहले के सामने लिंक जोड़ देगा लेकिन फिर दूसरी बार त्रुटि और बंद हो जाएगा, लिंक के बिना चार शब्द छोड़ देगा। कोई विचार?जावास्क्रिप्ट INDEX_SIZE_ERR: DOM अपवाद 1 श्रेणियों के लिए त्रुटि
if(searchString.length > 0) { // make sure the string isn't empty, or it'll crash.
// Search all text nodes
for(var i = 0; i < textNodes.length; i++) {
// Create a regular expression object to do the searching
var reSearch = new RegExp(searchString,'gmi'); // Set it to 'g' - global (finds all instances), 'm' - multiline (searches more than one line), 'i' - case insensitive
var stringToSearch = textNodes[i].textContent;
while(reSearch(stringToSearch)) { // While there are occurrences of the searchString
// Add the new selection range
var thisRange = document.createRange();
//alert((reSearch.lastIndex - searchString.length) + " <-> " + reSearch.lastIndex);
thisRange.setStart(textNodes[i], reSearch.lastIndex - searchString.length); // Start node and index of the selection range
thisRange.setEnd(textNodes[i], reSearch.lastIndex); // End node and index of the selection
var myLink = document.createElement('a');
var href = document.createAttribute('href');
myLink.setAttribute('href','http://www.google.com');
myLink.innerText ="GO";
thisRange.insertNode(myLink);
//theSelection.addRange(thisRange); // Add the node to the document's current selection
//thisRange.deleteContents();
}
}
}
सही ........ –
मुझे लगता है कि यह हो रहा है कि यह हो रहा है और रिवर्स ऑर्डर के बारे में सोचा। मुझे नहीं पता था कि ऐसा करने के बारे में कैसे जाना है, हालांकि, लूप्स RegExp ऑब्जेक्ट का उपयोग करते हुए थोड़ी देर लूप है। कोई सुझाव? – joshholat
आप अपने मैचों की अनुक्रमणिका को सरणी में स्टोर कर सकते हैं, और सरणी को एक अलग लूप में चला सकते हैं। मैंने अपना जवाब अपडेट कर लिया है। – gilly3