2015-10-17 51 views
0

使用以下代碼掃描頁面以查找匹配的單詞並將單詞轉換爲鏈接。document.body.innerHTML查殺事件需要備用解決方案

var linkWord = function(obj){ 
    for(i in obj){ 
    var x = document.body.innerHTML; 

    var linkStart = '<a href="'+obj[i]+'">'; 
    var linkEnd = '</a>'; 
    var reg = new RegExp("\\b(" + i + ")\\b(?![^<]*>|[^<>]*<\/[a])","ig"); 
    x = x.replace(reg, " " + linkStart + i + linkEnd + " "); 
    document.body.innerHTML = x; 
    console.log(document.body.innerHTML); 
    } 
    console.log(obj); 
} 

linkWord({ 
    'The':'http://www.example.com', 
    'Vokalia':'http://icant.co.uk', 
    'behind':'http://google.com', 
}); 

這個偉大的工程,但document.body.innerHTML是殺害頁面上的其他的JavaScript。任何人都可以提出另一個解決方案,實現相同的結果,不需要document.body.innerHTML?

這裏有一個小提琴:https://jsfiddle.net/91zux4ar/

無庫請。

+1

的可能的複製[如何動態地添加錨標記在JavaScript中一個div?](http://stackoverflow.com/questions/5519747/how-to- add-anchor-tags-dynamic-to-a-div-in-javascript) –

+0

不是重複的,那個問題的答案要求你知道要工作的元素類型或id。我的示例查找頁面中的所有匹配詞並創建鏈接。 – afro360

+0

您可以將鏈接附加到正文。 –

回答

2

https://jsfiddle.net/91zux4ar/10/

這是一個很大的研究和工作..但它是值得的結果。我想我會發布的jQuery插件

function wordToLinks(words) { 
    function getAllTextNodes(ele) { 
    var blackListedElements = ["a", "address", "area", "audio", "base", "br", "button", "canvas", "code", "data", "datalist", "embed", "fieldset", "form", "head", "hr", "html", "iframe", "img", "input", "ins", "keygen", "label", "link", "map", "math", "menu", "menuitem", "meta", "meter", "nav", "noscript", "object", "optgroup", "option", "output", "param", "pre", "progress", "rtc", "ruby", "samp", "script", "select", "source", "style", "svg", "textarea", "time", "title", "track", "var", "video", "wbr", "applet", "basefont", "dir", "font", "isindex", "noframes"]; 

    var nodes = []; 

    (function recrusive(element) { 
     var all_nodes = element.childNodes, 
      l = all_nodes.length, 
      child; 

     for (var i = 0; i < l; i++) { 
      child = all_nodes[i]; 
      if (child.nodeType == 3) { 
       nodes.push(child); 
      } else if (child.nodeType == 1 && blackListedElements.indexOf((child.tagName).toLowerCase()) < 0) { 
       recrusive(child); 
      } 
     } 

    })(ele); 
    return nodes.filter(function (e) { 
     return (/\w/).test(e.textContent); 
    }); 
} 

function createAtag(str, url) { 
    var tag = document.createElement('a'); 
    tag.innerText = str; 
    tag.href = url; 
    tag.style.display = 'inline'; 
    return tag; 
} 

function getIndex(node, word) { 
    //return node.nodeValue.toLowerCase().indexOf(word.toLowerCase()); 
    var reg = RegExp("\\b(" + word + ")\\b(?![^<]*>|[^<>]*<\/[a])","ig"); 
    var result = reg.exec(node.nodeValue); 
    var index = (result != undefined) ? result.index : -1; 
    return index; 
} 

var all_nodes = getAllTextNodes(document.body); 
var w = Object.keys(words); 


all_nodes.forEach(function (node) { 
    w.forEach(function (word) { 
     var c_node = node, 
      reg = new RegExp(word, 'i'), 
      tag, i; 

     i = getIndex(c_node, word); 

     while (i > -1) { 
      var second = c_node.splitText(i); 
      c_node = second.splitText(word.length); 
      tag = createAtag(second.nodeValue, words[word]) 
      second.parentElement.replaceChild(tag, second); 
      i = getIndex(c_node, word); 
     } 
    }); 
}); 

} 


var words = { 
    'the': 'http://www.example.com', 
     'Vokalia': 'http://icant.co.uk', 
     'behind': 'http://google.com', 
}; 

wordToLinks(words); 
+0

感謝所有的辛勤工作!但我不能得到它的詞:(heres小提琴https://jsfiddle.net/91zux4ar/ – afro360

+0

好 - 我很好,但找到了以下內容:它不是「全局」,因此它不會鏈接頁面中所有重複的單詞,它會覆蓋現有鏈接具有匹配的詞,如果有匹配(的)一詞如裏面的「母親」,將鏈接等等莫the [R – afro360

+0

我會嘗試做出一些改變,看看是否我可以計算出來:) – afro360