使用javascript bookmarklet查找并替换网页上class属性中的文本

使用javascript bookmarklet查找并替换网页上class属性中的文本,javascript,class,replace,find,bookmarklet,Javascript,Class,Replace,Find,Bookmarklet,我正在尝试创建一个javascript bookmarklet,该bookmarklet将查找文本集并将其替换为具有不同文本的类。例如,我想找到“shoe”并用“socks”替换,见下文 div class='鞋靴' 并将其替换为 div class='袜子' 我找到了这个: javascript:function%20htmlreplace(a,b,element){if(!element)element=document.body;var%20nodes=element.childNodes

我正在尝试创建一个javascript bookmarklet,该bookmarklet将查找文本集并将其替换为具有不同文本的类。例如,我想找到“shoe”并用“socks”替换,见下文

div class='鞋靴'

并将其替换为

div class='袜子'

我找到了这个:


javascript:function%20htmlreplace(a,b,element){if(!element)element=document.body;var%20nodes=element.childNodes;for(var%20n=0;n如果只有一个元素具有该类名,则可以使用

javascript:document.getElementByClassName(“shoe”).className=“socks”

但对于所有类,您都可以使用

var d=document.getElementByClassName(“shoe”);for(var i=0;i
javascript:function%20htmlreplace(a,b,element){if(!element)element=document.body;var%20nodes=element.childNodes;for(var%20n=0;n<nodes.length;n++){if(nodes[n].nodeType==Node.TEXT_NODE){nodes[n].textContent=nodes[n].textContent.replace(new%20RegExp(a,'gi'),b);}else{htmlreplace(a,b,nodes[n]);}}}htmlreplace(prompt("Text%20to%20replace:","old"),prompt("Replacement%20text:","new"));