书签帮助:创建查找/替换书签 [英] Bookmarklet Help: Creating a Find/Replace Bookmarklet

查看:94
本文介绍了书签帮助:创建查找/替换书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试略微修改,以便它提示要搜索的文本,然后是文本替换为,并在完成所有处理后,显示一个对话框,让我知道它已完成.

I'm trying to slightly modify this so that it prompts for the text to search for, followed by the text to replace with, and when all done processing, show a dialog box letting me know it's done.

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

I plan to use it on a phpmyadmin database edit page that'll have any number of textboxes filled with text (which is what I need it to search and replace in). Also, the text to search for and replace may or may not be multi-line, so I've added the 'm' param in the regex, and also, since I'll be doing searches/replaces that may contain html, they'll often have quotes/double quotes in them. ex:

搜索:

<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>

,也许什么也不要替换(只是为了删除所有代码)或其他一些html.到目前为止,这是我想出的小书签(( javascript,尤其是我经常不喜欢的小书签).但是,尽管它确实找到/替换了它,但它并没有任何作用.正确地进行提示.

and maybe replace with nothing (just to erase all that code), or some other html. So far this is the bookmarklet I've come up with, (javascript, and especially bookmarklets aren't something I mess with often) however, it does nothing as far as finding/replacing, although it does do the prompting correctly.

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:',''));

有人有什么主意吗?

推荐答案

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

Here is the most direct conversion of the original function to search/replace textarea and text inputs instead of HTML. I also added 'm' to regex and added alert('done') at end. However, I think that using 'm' may not solve your problem perfectly, but I may be wrong.

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

这里是作为书签.

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

这篇关于书签帮助:创建查找/替换书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆