查找并替换整个页面

发布于 2024-12-09 19:36:33 字数 2827 浏览 0 评论 0原文

我正在尝试为整个页面创建查找和替换。

我正在使用 findandreplace 这是一个迷你插件,它是:

function findAndReplace(searchText, replacement, searchNode) {
    if (!searchText || typeof replacement === 'undefined') {
       alert("Please Enter Some Text Into the Field");
        return;
    }
    var regex = typeof searchText === 'string' ?
                new RegExp(searchText, 'g') : searchText,
        childNodes = (searchNode || document.body).childNodes,
        cnLength = childNodes.length,
        excludes = 'html,head,style,title,link,meta,script,object,iframe';
    while (cnLength--) {
        var currentNode = childNodes[cnLength];
        if (currentNode.nodeType === 1 &&
            (excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) {
            arguments.callee(searchText, replacement, currentNode);
        }
        if (currentNode.nodeType !== 3 || !regex.test(currentNode.data) ) {
            continue;
        }
        var parent = currentNode.parentNode,
            frag = (function(){
                var html = currentNode.data.replace(regex, replacement),
                    wrap = document.createElement('div'),
                    frag = document.createDocumentFragment();
                wrap.innerHTML = html;
                while (wrap.firstChild) {
                    frag.appendChild(wrap.firstChild);
                }
                return frag;
            })();
        parent.insertBefore(frag, currentNode);
        parent.removeChild(currentNode);
    }
}

目前我需要查找按钮为找到的每个项目创建一个单独的 JS。我已经做到了,因此它为每个找到的项目添加了不同的随机 ID。现在我只需要JS。当我完成整个这一切时,我会想要它,所以当您单击找到的单词之一时,它会弹出一个弹出窗口(不是警报),显示:“将单词替换为:{INPUT HERE}”然后它将仅替换该单词。我想那会很酷。

我的查找代码是:

document.getElementById('find').onsubmit = function() {
    findAndReplace(document.getElementById('fText').value, function(hi) {
        var n = Math.floor(Math.random() * 9999999999);
        var s = Math.floor(Math.random() * 30);

        $('#fade').after('<span id="show' + n + '" style="display:none;"></span>');
        $('#show' + n + '').html(hi);
        $('body').prepend("<script type='text/javascript'>$('#highlight" + n + "').click(function(){$('#fade').show();});$('#highlight" + n + "').mouseover(function(){$('#highlight" + n + "').css('background', 'blue');});$('#highlight" + n + "').mouseout(function(){$('#highlight" + n + "').css('background', 'yellow');});</sc" + "ript>");
        return '<span id="highlight' + n + '" style="background: yellow;color: black;">' + hi + '</span>';

    });
    return false;
};

我发现 .html 无法将

我希望有人可以提供帮助。谢谢。

I'm trying to create a Find and Replace for the whole page.

I'm Using findandreplace which is a mini-plugin which is:

function findAndReplace(searchText, replacement, searchNode) {
    if (!searchText || typeof replacement === 'undefined') {
       alert("Please Enter Some Text Into the Field");
        return;
    }
    var regex = typeof searchText === 'string' ?
                new RegExp(searchText, 'g') : searchText,
        childNodes = (searchNode || document.body).childNodes,
        cnLength = childNodes.length,
        excludes = 'html,head,style,title,link,meta,script,object,iframe';
    while (cnLength--) {
        var currentNode = childNodes[cnLength];
        if (currentNode.nodeType === 1 &&
            (excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) {
            arguments.callee(searchText, replacement, currentNode);
        }
        if (currentNode.nodeType !== 3 || !regex.test(currentNode.data) ) {
            continue;
        }
        var parent = currentNode.parentNode,
            frag = (function(){
                var html = currentNode.data.replace(regex, replacement),
                    wrap = document.createElement('div'),
                    frag = document.createDocumentFragment();
                wrap.innerHTML = html;
                while (wrap.firstChild) {
                    frag.appendChild(wrap.firstChild);
                }
                return frag;
            })();
        parent.insertBefore(frag, currentNode);
        parent.removeChild(currentNode);
    }
}

At the moment I need the find button to make a seperate JS for each Item found. I have made it so it adds a different random ID for each found item. Now I just need the JS. When Im done with the whole this I will want it so when you click on one of the found words it has a popup(not alert) saying: "Replace Word with: {INPUT HERE}" Then it will replace just that word. I thought that would be cool.

My find code is:

document.getElementById('find').onsubmit = function() {
    findAndReplace(document.getElementById('fText').value, function(hi) {
        var n = Math.floor(Math.random() * 9999999999);
        var s = Math.floor(Math.random() * 30);

        $('#fade').after('<span id="show' + n + '" style="display:none;"></span>');
        $('#show' + n + '').html(hi);
        $('body').prepend("<script type='text/javascript'>$('#highlight" + n + "').click(function(){$('#fade').show();});$('#highlight" + n + "').mouseover(function(){$('#highlight" + n + "').css('background', 'blue');});$('#highlight" + n + "').mouseout(function(){$('#highlight" + n + "').css('background', 'yellow');});</sc" + "ript>");
        return '<span id="highlight' + n + '" style="background: yellow;color: black;">' + hi + '</span>';

    });
    return false;
};

I figured out that .html cant place <script> tags into a document so I hope there is another way I can do this.
Its a little crazy looking. Mabye you can see it better and know what I want here

I hope someone can help. Thank you.

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

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

发布评论

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

评论(1

岛徒 2024-12-16 19:36:33

我在脚本末尾添加了以下代码。它从提示中获取输入,但它会“替换所有内容”,而不仅仅是您单击的内容。

$('span[id^=highlight]').live('click', function () {
    replacePrompt = prompt('Replace Word with:','{INPUT HERE}');
    findAndReplace(document.getElementById('fText').value, function() {
        return '<span class="highlight2" style="background: white;color: black;">' + replacePrompt + '</span>';
    });
    return false;
})

也许我可以在完成您的 findAndReplace 函数后解决一些问题。

更新:解决方案是根本不使用 findAndReplace 插件。我仍然不确定,但这可能就是你想要的。

$('span[id^=highlight]').live('click', function () {
    replacePrompt = prompt('Replace Word with:','{INPUT HERE}');
    $(this).text(replacePrompt);
})

I added the following code at the end of your script. It takes input from a prompt but it will 'replace all', not just the one that you click.

$('span[id^=highlight]').live('click', function () {
    replacePrompt = prompt('Replace Word with:','{INPUT HERE}');
    findAndReplace(document.getElementById('fText').value, function() {
        return '<span class="highlight2" style="background: white;color: black;">' + replacePrompt + '</span>';
    });
    return false;
})

Maybe I can work something out after going through your findAndReplace function..

UPDATE: A solution is to not use the findAndReplace plugin at all. I am still not sure but this is probably what you want.

$('span[id^=highlight]').live('click', function () {
    replacePrompt = prompt('Replace Word with:','{INPUT HERE}');
    $(this).text(replacePrompt);
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文