четвъртък, 19 юли 2007 г.

JavaScript: Replace Text Found in a Node and its Subnodes of the DOM Tree

The following examples replaces the letter "l" or "L" with "r" or "R" respectively. It gets a DOM element, and traverses it along with its children, and performs the operation.

function engrish(n)
{
if(n.nodeType == document.TEXT_NODE)
{
n.nodeValue = n.nodeValue.replace(/l/g, 'r').replace(/L/g, 'R');
}
else if(n.hasChildNodes())
{
for(var i=0; i < n.childnodes.length; i++)
engrish(n.childNodes[i]);
}
}
}

Няма коментари: