Javascript 查找并替换整个页面
我正在尝试为整个页面创建查找和替换 我正在使用findandreplace,这是一个小型插件,它是:Javascript 查找并替换整个页面,javascript,jquery,random,replace,find,Javascript,Jquery,Random,Replace,Find,我正在尝试为整个页面创建查找和替换 我正在使用findandreplace,这是一个小型插件,它是: function findAndReplace(searchText, replacement, searchNode) { if (!searchText || typeof replacement === 'undefined') { alert("Please Enter Some Text Into the Field"); re
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;
};
document.getElementById('find')。onsubmit=function(){
findAndReplace(document.getElementById('fText')。值,函数(hi){
var n=Math.floor(Math.random()*9999999);
var s=Math.floor(Math.random()*30);
$('#褪色')。在('')之后;
$('#show'+n+'').html(hi);
$('body')。前置($('highlight“+n+”)。单击(函数(){$('fade').show()})$('highlight“+n+”)。鼠标悬停(函数(){$('highlight“+n+”).css('background',blue');$('highlight“+n+”))。鼠标悬停(函数(){('('highlight highlight“+n+”))))。css('background',blue'));$);$)。css('background蓝')));$);$)。鼠标悬停(函数(){('highlight背景()('highlight n+)));+n+);
返回“+hi+”;
});
返回false;
};
我发现.html不能将
标记放入文档中,所以我希望有另一种方法可以做到这一点。
看起来有点疯狂。也许你能看得更清楚,知道我想要什么
我希望有人能帮忙。谢谢。我在脚本末尾添加了以下代码。它接受来自提示符的输入,但它将“全部替换”,而不仅仅是您单击的那个
$('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;
})
我在脚本末尾添加了以下代码。它接受来自提示符的输入,但它将“全部替换”,而不仅仅是您单击的那个
$('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功能不准确,请尝试使用“a”或锚定标记。它将失败。您应该通过使用excludes=['html'、'head'、'style'、'title'、'link'、'meta'、'script'、'object'、'iframe']替换excludes来改进其排除方法;使用includes而不是indexOf在数组中进行匹配,如下所示!排除.includes(currentNode.nodeName.toLowerCase())。这与主题无关,但将帮助您改进遍历方法。上述findAndReplace函数不准确请尝试使用“a”或锚定标记。它将失败。您应该通过使用excludes=['html'、'head'、'style'、'title'、'link'、'meta'、'script'、'object'、'iframe']替换excludes来改进其排除方法;使用includes而不是indexOf在数组中进行匹配,如下所示!排除.includes(currentNode.nodeName.toLowerCase())。这是离题的,但将帮助您改进遍历方法。