简体   繁体   English

Javascript INDEX_SIZE_ERR:DOM异常1范围错误

[英]Javascript INDEX_SIZE_ERR: DOM Exception 1 Error for ranges

Using the following code, I get a INDEX_SIZE_ERR: DOM Exception 1 error on the thisRange.setStart line. 使用以下代码,我在thisRange.setStart行上得到INDEX_SIZE_ERR:DOM异常1错误。 The code is meant to go through a whole page, find instances of the searchString, and then add a link in front of that search string. 该代码旨在遍历整个页面,查找searchString的实例,然后在该搜索字符串之前添加一个链接。 For example, if it finds 5 instances of the string, right now it will add the link in front of the first one but then error on the second and stop, leaving four words without the link. 例如,如果它找到该字符串的5个实例,则现在它将在第一个实例的前面添加链接,但是在第二个实例的前面添加错误,然后停止,从而使四个单词没有链接。 Any ideas? 有任何想法吗?

    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();
        }
    }
}

Once you've added a link, the document has changed. 添加链接后,文档已更改。 When you next call thisRange.setStart , it's using the index from the original string, but setting it in the now changed document. 下次调用thisRange.setStart ,它将使用原始字符串中的索引,但会在现在更改的文档中进行设置。

You need to add them in reverse order. 您需要以相反的顺序添加它们。 Try storing the match indexes in an array, and then walk your array of indexes backwards to inject your links. 尝试将匹配索引存储在数组中,然后向后移动索引数组以注入链接。

I figured it out. 我想到了。 Here's how: 这是如何做:

    for (var i = rangeArray.length - 1; i >= 0; i--) {
    var myLink = document.createElement('a'); 
    var href = document.createAttribute('href'); 
    myLink.setAttribute('href','http://www.google.com'); 
    myLink.innerText ="GO";
    rangeArray[i].insertNode(myLink);
}

Instead of adding it to the range in the above loop, I added it to and array and then went through that array backwards. 我没有将其添加到上述循环中的范围,而是将其添加到and数组,然后向后遍历该数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM