Replace a line - how to replace each word once

I have an xml dictionary as shown below.

<word definition = "The primary income-earner in a household" & gtbread winner & lt / word & gt
<word definition = "One who wins, or gains by success in competition, contest, or gaming" & gtwinner & lt / word & gt

When there is a word from the dictionary in my html, this word will be replaced by the link and the definition as title. When the link freezes, the user should see the definition.

var allwords = xmlDoc.getElementsByTagName ("word");
for (var i = 0; i <allwords.length; i ++)
    {
            var name = allwords [i] .lastChild.nodeValue;

            var linked = '<a href ="#" title="' + allwords[ii.lastChild.nodeValue +':' + allwords[i†.getAttribute("definition") +'"> '+ allwords [i] .lastChild.nodeValue + '</a>'; 
    }

Here is my substitute

function replacer (oldstring, newstring) {
document.body.innerHTML = document.body.innerHTML.replace (oldstring, newstring);
}

But the problem is once the bread winner changes to a connected form, it will also win , since the bread winner includes the winner , the winner changes twice, and the whole code is mixed.

I ask if there is a way, after the winner of the bread changes, the winner should no longer change.

!

+3
4

- :

for (var i=0; i<allwords.length; i++)
{
    if(allwords[i].firstChild.name == 'a') {
        // This word has been linked already, skip it
    }

    // your code
}
+2

- , . (, html, , , html). , , .

0

You will need to iterate through the appropriate text nodes and replace only those that do not have the A tag as an ancestor in the DOM.

0
source

Try the strtr function ($ text, $ fromList, $ toList). It must replace each term once.

-1
source

All Articles