突出显示电话号码并用标记javascript包装

突出显示电话号码并用标记javascript包装,javascript,internet-explorer,replace,innerhtml,highlighting,Javascript,Internet Explorer,Replace,Innerhtml,Highlighting,下面的代码检查所选标记是否具有子节点。如果存在子节点,它将循环直到找到子节点。当没有找到更多的子节点时,它会循环,即它到达一个文本节点,导致循环结束。函数被设置为递归运行,直到找不到子节点为止。代码按照上述信息运行,但当我尝试匹配文本节点时(console.log()输出所有文本节点),replace()用于使用regex识别电话号码,并替换为超链接。该数字被检测到并用超链接括起来,但它会显示两次,即用超链接括起来的数字和唯一的数字。以下是代码 function DOMw

下面的代码检查所选标记是否具有子节点。如果存在子节点,它将循环直到找到子节点。当没有找到更多的子节点时,它会循环,即它到达一个文本节点,导致循环结束。函数被设置为递归运行,直到找不到子节点为止。代码按照上述信息运行,但当我尝试匹配文本节点时(
console.log()
输出所有文本节点),
replace()
用于使用regex识别电话号码,并替换为超链接。该数字被检测到并用超链接括起来,但它会显示两次,即用超链接括起来的数字和唯一的数字。以下是代码

            function DOMwalker(obj){
            var regex = /\+\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}/g;
             var y = "<a href=\"javascript:void(0);\">$&</a>";
            if(obj.hasChildNodes()){
                var child = obj.firstChild;

                while(child){
                    if(child.nodeType!==3)
                    {
                        DOMwalker(child);
                    }
                    if (child.nodeType=== 3) {
                            var text = child.nodeValue;
                             console.log(typeof text);                            
                            var regs = regex.exec(text);

                            match = text.replace(regex,y);

                            if(match){

                                var item = document.createElement('a');
                                item.setAttribute('href','javascript:void(0);');
                                var detect = document.createTextNode(match);
                                var x=item.appendChild(detect);
                                console.log(x);
                               child.parentNode.insertBefore(x,child);
                           }
                     }

                     child=child.nextSibling;
                }
            }
        };
        $(window).load(function(){
            var tag = document.querySelector(".gcdMainDiv div.contentDiv");

            DOMwalker(tag);
        });
函数DOMwalker(obj){
var regex=/\+\d{1,4}.[-.\s]?\(?\d{1,3}.\)?[-.\s]?\d{1,4}.[-.\s]?\d{1,4}.[-.\s]?\d{1,9}/g;
变量y=“”;
if(obj.hasChildNodes()){
var child=obj.firstChild;
while(儿童){
if(child.nodeType!==3)
{
多姆沃克(儿童);
}
if(child.nodeType==3){
var text=child.nodeValue;
console.log(文本类型);
var regs=regex.exec(文本);
匹配=文本。替换(正则表达式,y);
如果(匹配){
var item=document.createElement('a');
item.setAttribute('href','javascript:void(0);');
var detect=document.createTextNode(匹配);
var x=项。追加子项(检测);
控制台日志(x);
parentNode.insertBefore(x,child);
}
}
child=child.nextSibling;
}
}
};
$(窗口)。加载(函数(){
var tag=document.querySelector(“.gcdMainDiv.contentDiv”);
多姆沃克(tag);
});
以下是输出的屏幕截图:

在这里,数字被打印两次,而不是一次显示超链接(预期带有超链接的突出显示数字)和第二个widout标记

以下是
x

我已经经历过了


提供的解决方案与FF配合使用效果良好。在IE11中使用时会出现问题。它抛出
未知运行时错误
,并引用
.innerHTML
。我使用了appenChild(),但错误无法解决。

您发布的内容有几个问题。首先,如果子节点不是节点类型3,也不是脚本节点,则重新调用
recursivetree()
,但不传入子节点。函数将在第一个div元素处重新开始,然后再次无限循环

其次,您正在对节点本身调用
replace()
,而不是对节点的
innerHTML
。您试图用字符串替换一个节点,但这行不通,我认为您的意思是替换该节点中的任何匹配数字,而不是整个节点

如果您有
我的号码是+111-555-9999
,您只想更换号码,而不想丢失所有其他号码

尝试以下解决方案:

function recursivetree(obj){
   var regex = /\+\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}/g;
   var y = "<a href=\"javascript:;\">$&</a>";
   var obj = obj || document.getElementsByTagName('div')[0];
   if(obj.hasChildNodes()){
       var child = obj.firstChild;
       while(child){
           if(child.nodeType !== 3 && child.nodeName !== 'SCRIPT'){
                  //Recall recursivetree with the child 
                  recursivetree(child);
           }

           //A nodeType of 3, text nodes, sometimes do not have innerHTML to replace
           //Check if the child has innerHTML and replace with the regex
           if (child.innerHTML !== undefined) {
                child.innerHTML = child.innerHTML.replace(regex,y);
           }
           child=child.nextSibling;
       }
   }
}
recursivetree();
函数递归树(obj){ var regex=/\+\d{1,4}.[-.\s]?\(?\d{1,3}.\)?[-.\s]?\d{1,4}.[-.\s]?\d{1,4}.[-.\s]?\d{1,9}/g; 变量y=“”; var obj=obj | | document.getElementsByTagName('div')[0]; if(obj.hasChildNodes()){ var child=obj.firstChild; while(儿童){ if(child.nodeType!==3&&child.nodeName!==SCRIPT){ //和孩子一起回忆递归树 递归树(子); } //一个节点类型为3的文本节点,有时没有要替换的innerHTML //检查子项是否具有innerHTML并替换为正则表达式 if(child.innerHTML!==未定义){ child.innerHTML=child.innerHTML.replace(regex,y); } child=child.nextSibling; } } } 递归树(); 小提琴:

老实说?如果您试图循环浏览整个页面并替换数字的所有实例,只需在主体上执行替换即可

var regex = /\+\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}/g;
var y = "<a href=\"javascript:;\">$&</a>";
var body = document.body;
body.innerHTML = body.innerHTML.replace(regex, y);
var regex=/\+\d{1,4}.[-.\s]?\(?\d{1,3}?)?[-.\s]?\d{1,4}.[-.\s]?\d{1,4}.[-.\s]?\d{1,9}/g;
变量y=“”;
var body=document.body;
body.innerHTML=body.innerHTML.replace(regex,y);

小提琴:

最后,我得到了问题的答案。我提到这一点,这帮助我解决了我的疑问

代码如下:

            function DOMwalker(obj){
            if(obj.hasChildNodes()){
                var child = obj.firstChild;
                var children = obj.childNodes;
                var length = children.length;
                for(var i = 0;i<length;i++){
                    var nodes = children[i];
                    if(nodes.nodeType !==3){
                        DOMwalker(nodes);
                    }
                    if(nodes.nodeType===3){
                        //Pass the parameters nodes:current node being traversed;obj:selector being passed as parameter for DOMwalker function 
                        highlight(nodes,obj);
                    }
                }
                child = child.nextSibling;
            }
        }
        function highlight(node,parent){
            var regex =/(\d{1}-\d{1,4}-\d{1,5})|(\+\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9})/g;
            //Stores the value of current node which is passed through the if loop
            var matchs = node.data.match(regex);
            if matchs is true,add it to DOM
            if(matchs){
                var anchor = document.createElement("a");
                var y = /[(]\d[)]|[.-\s]/g;//removes spaces periods or dash,also number within brackets
                var remove = number.replace(y,'');
                //tel uri,if you have an app like skype for click-to dial
                anchor.setAttribute("href","tel:"+remove);
                //the anchor tag should be inserted before in the current node in the DOM
                parent.insertBefore(anchor,node);
                //append it toh the DOM to be displaye don the web page
                anchor.appendChild(node);

            }
            else
            {
                return false;
            }

        }
函数DOMwalker(obj){
if(obj.hasChildNodes()){
var child=obj.firstChild;
var children=obj.childNodes;
变量长度=childrence.length;

对于(var i=0;iPerhaps您刚才复制并粘贴了错误的代码,但看起来您的while块中有一个无限循环。while括号是否应在
child=child.nextSibling
之后结束?能否提供一些您希望匹配但失败的电话号码示例?@abmitchell:修复了它…如果
loop=]@talemyn:+11 111 11-11111+11 1111 3333333我不确定你想要完成的是什么。你能发布你试图操纵的页面的HTML,以及你操纵后的最终HTML结果是什么吗?我不建议使用。innerhtml。使用你上面的代码阅读此内容只会使网页上的内容消失我正在为IE开发一个插件,用于在网页上突出显示电话号码。因此,网络上的原始内容不应受到干扰。我希望你明白我的意思。使用.innerHTML属性很好,你提供的链接