Javascript 如何删除特定文本短语的所有实例?
在网页的主体区域是唯一可访问的部分的情况下,是否有方法使用内联JavaScript或其他内联语言删除特定文本短语(以HTML编写)的所有实例 这在许多情况下都很有用,例如人们使用Tiny.cc/customurl,并希望删除声明“Tiny.cc/”的部分Javascript 如何删除特定文本短语的所有实例?,javascript,dom,Javascript,Dom,在网页的主体区域是唯一可访问的部分的情况下,是否有方法使用内联JavaScript或其他内联语言删除特定文本短语(以HTML编写)的所有实例 这在许多情况下都很有用,例如人们使用Tiny.cc/customurl,并希望删除声明“Tiny.cc/”的部分 如果允许,我们将使用Tiny.cc修改日历插件,以创建自定义URL(Tiny.cc/customurl)。该插件默认显示完整的URL,因此我们希望去掉文本“tiny.cc/”并在代码中保留“customurl”部分: <div clas
如果允许,我们将使用Tiny.cc修改日历插件,以创建自定义URL(Tiny.cc/customurl)。该插件默认显示完整的URL,因此我们希望去掉文本“tiny.cc/”并在代码中保留“customurl”部分:
<div class="ews_cal_grid_custom_item_3">
<div class="ews_cal_grid_select_checkbox_clear" id="wGridTagChk" onclick="__doPostBack('wGridTagChk', 'tiny.cc/Baseball-JV');" > </div>
tiny.cc/Baseball-JV
</div>
tiny.cc/barball-JV
我们要删除的部分是
http://tiny.cc/
在第三行中单独显示。只要可以以字符串格式提供数据,就可以使用正则表达式来完成此操作
您可以解析body标记的整个innerHTML,如果您只能访问这些内容的话。这是一种缓慢且有点糟糕的练习方法,但为了解释起见:
document.body.innerHTML = document.body.innerHTML.replace(
/http:\/\/tiny\.cc\//i, // The regular expression to search for
""); // Waht to replace with (nothing).
整个表达式包含在正斜杠中,因此regexp中的任何正斜杠都需要用反斜杠转义
这适用于在regexp中具有特殊含义的其他字符,例如句点。单个句点(
)表示匹配的“任意”字符。要匹配句点,必须对其进行转义(\.
)
编辑:
如果希望在onclick中保留对URL的引用,可以将regexp修改为在单引号内不匹配(例如):
如果不想替换HTML中该字符串的所有实例,则必须递归迭代节点结构,例如:
function textFilter(element, search, replacement) {
for (var i = 0; i < element.childNodes.length; i++) {
var child = element.childNodes[i];
var nodeType = child.nodeType;
if (nodeType == 1) { // element
textFilter(child, search, replacement);
} else if (nodeType == 3) { // text node
child.nodeValue = child.nodeValue.replace(search, replacement);
}
}
}
要做到这一点而不替换所有HTML(这会破坏所有事件处理程序),也要做到这一点而不使用递归(通常更快),可以执行以下操作:
function removeText(top, txt) {
var node = top.firstChild, index;
while(node && node != top) {
// if text node, check for our text
if (node.nodeType == 3) {
// without using regular expressions (to avoid escaping regex chars),
// replace all copies of this text in this text node
while ((index = node.nodeValue.indexOf(txt)) != -1) {
node.nodeValue = node.nodeValue.substr(0, index) + node.nodeValue.substr(index + txt.length);
}
}
if (node.firstChild) {
// if it has a child node, traverse down into children
node = node.firstChild;
} else if (node.nextSibling) {
// if it has a sibling, go to the next sibling
node = node.nextSibling;
} else {
// go up the parent chain until we find a parent that has a nextSibling
// so we can keep going
while ((node = node.parentNode) != top) {
if (node.nextSibling) {
node = node.nextSibling;
break;
}
}
}
}
}
在此进行工作演示:
要对整个文档执行此操作,只需调用:
removeText(document.body, "http://tiny.cc/Baseball-JV");
这是一个很好的开始,但我需要保留tiny.cc的onclick=“uu doPostBack(“…,”);”实例,因为它提供了链接。@beta208您会发现,这实际上将替换整个正文HTML中的所有regexp实例。我如何在div的纯文本中删除它,而不是div的onclick属性?嘿,太好了,我将尝试一下,看看是否可以将其作为内联JS使用。为了清晰起见,正则表达式textFilter中的g是什么?它是“全局”修饰符。如果省略它,那么每个文本节点只替换一个实例:哦,很好,谢谢。此函数部分是否可以内联运行?我无法修改head以包含外部js文件。当递归调用textFilter时,我认为第二个参数应该是
search
,而不是regex
。在无递归树漫游中的一个不错的参数。我不认为速度的差异值得可读性,但我喜欢这种设计。@cloudfeet-这取决于文档的长度和深度以及速度的重要性。注意:这也是一个通用函数,可用于任何文本字符串,即使其中包含regex字符。但是,在没有递归的情况下,确实需要更多的代码。这个函数部分能够内联运行吗?我不能修改头部以包含外部js文件。@beta208-是,它可以是内联的。必须等到DOM加载完成后才能运行它。最简单的方法是window.onload=function(){removeText(document.body,“http://tiny.cc/Baseball-JV");};如果其他代码正在使用onload
,则使用事件侦听器。事实上,它在JSFIDLE I链接中是内联的(在
部分的末尾)。@cloudfeets-随着一个更大的文档,差异会显示得更多,它看起来像非递归版本是2x
(Chrome)到5x
(Safari)到9x
(IE)更快。我同意如果可能的话,将搜索范围限制在文档的目标部分会很有用。
function removeText(top, txt) {
var node = top.firstChild, index;
while(node && node != top) {
// if text node, check for our text
if (node.nodeType == 3) {
// without using regular expressions (to avoid escaping regex chars),
// replace all copies of this text in this text node
while ((index = node.nodeValue.indexOf(txt)) != -1) {
node.nodeValue = node.nodeValue.substr(0, index) + node.nodeValue.substr(index + txt.length);
}
}
if (node.firstChild) {
// if it has a child node, traverse down into children
node = node.firstChild;
} else if (node.nextSibling) {
// if it has a sibling, go to the next sibling
node = node.nextSibling;
} else {
// go up the parent chain until we find a parent that has a nextSibling
// so we can keep going
while ((node = node.parentNode) != top) {
if (node.nextSibling) {
node = node.nextSibling;
break;
}
}
}
}
}
removeText(document.body, "http://tiny.cc/Baseball-JV");