Javascript INDEX_SIZE_ERR:DOM 异常 1 范围错误

发布于 2024-09-27 09:03:07 字数 1638 浏览 0 评论 0原文

使用以下代码,我在 thisRange.setStart 行上收到 INDEX_SIZE_ERR: DOM Exception 1 错误。该代码旨在遍历整个页面,查找 searchString 的实例,然后在该搜索字符串前面添加一个链接。例如,如果它找到该字符串的 5 个实例,现在它将在第一个实例前面添加链接,但在第二个实例上出错并停止,留下四个没有链接的单词。有什么想法吗?

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

Using the following code, I get a INDEX_SIZE_ERR: DOM Exception 1 error on the thisRange.setStart line. 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. 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. 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();
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残月升风 2024-10-04 09:03:07

添加链接后,文档就发生了更改。当您下次调用 thisRange.setStart 时,它将使用原始字符串中的索引,但将其设置在现在更改的文档中。

您需要以相反的顺序添加它们。尝试将匹配索引存储在数组中,然后向后遍历索引数组以注入链接。

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.

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.

千纸鹤带着心事 2024-10-04 09:03:07

我想通了。方法如下:

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

我没有将其添加到上面循环中的范围,而是将其添加到数组中,然后向后遍历该数组。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文