小书签帮助:创建查找/替换小书签

J. *_*ein 5 javascript replace bookmarklet

我正在尝试对此进行稍微修改,以便它提示输入要搜索的文本,然后提示要替换的文本,当所有处理完成后,显示一个对话框,让我知道它已完成。

我计划在 phpmyadmin 数据库编辑页面上使用它,该页面将有任意数量的填充文本的文本框(这是我需要它在 中搜索和替换的内容)。另外,要搜索和替换的文本可能是也可能不是多行,因此我在正则表达式中添加了“m”参数,而且,由于我将进行可能包含 html 的搜索/替换,因此它们里面经常会有引号/双引号。前任:

搜索:

<img height="76" width="92" src="http://www.gifs.net/Animation11/Hobbies_and_Entertainment/Games_and_Gambling/Slot_machine.gif" /></div>
<div class="rtecenter"> <strong><em><font color="#ff0000">Vegas Baby!<br />
</font></em></strong></div>
Run Code Online (Sandbox Code Playgroud)

也许什么都不替换(只是为了删除所有代码),或者其他一些 html。到目前为止,这是我想出的小书签(javascript,尤其是小书签不是我经常弄乱的东西),但是,它在查找/替换方面没有任何作用,尽管它确实正确地进行了提示。

javascript:var%20scrEl=document.createElement('script');scrEl.setAttribute('language','javascript');scrEl.setAttribute('type','text/javascript');scrEl.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js');function%20htmlreplace(a,b,element){if(!element)element=document.body;var%20nodes=$(element).contents().each(function(){if(this.nodeType==Node.TEXT_NODE){var%20r=new%20RegExp(a,'gim');this.textContent=this.textContent.replace(r,b);}else{htmlreplace(a,b,this);alert('Done%20processing.');}});}htmlreplace(prompt('Text%20to%20find:',''),prompt('Replace%20with:',''));
Run Code Online (Sandbox Code Playgroud)

有人有主意吗?

DG.*_*DG. 5

这是原始函数最直接的转换,用于搜索/替换文本区域和文本输入而不是 HTML。我还在正则表达式中添加了“m”,并在末尾添加了警报(“完成”)。但是,我认为使用“m”可能无法完美解决您的问题,但我可能是错的。

function htmlreplace(a, b, element) {
    if (!element) element = document.body;    
    var nodes = element.childNodes;
    for (var n=0; n<nodes.length; n++) {
        if ( nodes[n].type && (nodes[n].type.toLowerCase() == 'textarea' || nodes[n].type.toLowerCase() == 'text') ) {
            var r = new RegExp(a, 'gim');
            nodes[n].value = nodes[n].value.replace(r, b);
        } else {
            htmlreplace(a, b, nodes[n]);
        }
    }
}

htmlreplace(prompt('find'), prompt('replace'));
alert('done');
Run Code Online (Sandbox Code Playgroud)

这是一个书签。

javascript:function htmlreplace(a,b,element){if(!element)element=document.body;var nodes=element.childNodes;for(var n=0;n<nodes.length;n++){if(nodes[n].type&&(nodes[n].type.toLowerCase()=='textarea'||nodes[n].type.toLowerCase()=='text')){var r=new RegExp(a,'gim');nodes[n].value=nodes[n].value.replace(r,b)}else{htmlreplace(a,b,nodes[n])}}}htmlreplace(prompt('find'),prompt('replace'));alert('done');
Run Code Online (Sandbox Code Playgroud)