编程语言
首页 > 编程语言> > javascript – Bookmarklet帮助:创建查找/替换书签

javascript – Bookmarklet帮助:创建查找/替换书签

作者:互联网

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

我计划在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>

并且可能没有替换(只是为了擦除所有代码),或者其他一些html.到目前为止,这是我提出的书签,(javascript,特别是bookmarklet不是经常搞乱的东西)但是,它找不到任何东西,只要找到/替换,虽然它确实正确地提示.

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

有人有想法么?

解决方法:

这是原始函数最直接的转换,用于搜索/替换textarea和文本输入而不是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');

这是一个书签.

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

标签:bookmarklet,javascript,replace
来源: https://codeday.me/bug/20191007/1865019.html