title image


Smiley Re: Durch alle Nodes iterieren
Hi,



ich hab' nen bißchen in Google gesucht (javascript replace text nodes recursive) und auf eine Funktion gestoßen, die rekursiv in allen Nodes den Text/ die Zeichendaten größerstellt (uppercase). Diese Funktion habe ich dann so angepasst, dass sie Texte auf der ganzen Seite austauscht, in diesem Beispiel para mit neuerText. Die replaceSubstring - Funktion verwende ich hier, um komfortabler Text auszutauschen.







<html>

<head>

<script language="JavaScript">

<!--



function replaceSubstring(inputString, fromString, toString) {

// Goes through the inputString and replaces every occurrence of fromString with toString

var temp = inputString;

if (fromString == "") {

return inputString;

}

if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)

while (temp.indexOf(fromString) != -1) {

var toTheLeft = temp.substring(0, temp.indexOf(fromString));

var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);

temp = toTheLeft + toString + toTheRight;

}

} else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop

var midStrings = new Array("~", "`", "_", "^", "#");

var midStringLen = 1;

var midString = "";

// Find a string that doesn't exist in the inputString to be used

// as an "inbetween" string

while (midString == "") {

for (var i=0; i < midStrings.length; i++) {

var tempMidString = "";

for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }

if (fromString.indexOf(tempMidString) == -1) {

midString = tempMidString;

i = midStrings.length + 1;

}

}

} // Keep on going until we build an "inbetween" string that doesn't exist

// Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string

while (temp.indexOf(fromString) != -1) {

var toTheLeft = temp.substring(0, temp.indexOf(fromString));

var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);

temp = toTheLeft + midString + toTheRight;

}

// Next, replace the "inbetween" string with the "toString"

while (temp.indexOf(midString) != -1) {

var toTheLeft = temp.substring(0, temp.indexOf(midString));

var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);

temp = toTheLeft + toString + toTheRight;

}

} // Ends the check to see if the string being replaced is part of the replacement string or not

return temp; // Send the updated string back to the user

} // Ends the "replaceSubstring" function



function replace(n,from,to) {

/*



1 Elementknoten

2 Attributknoten

3 Textknoten

4 Knoten für CDATA-Bereich

5 Knoten für Entity-Referenz

6 Knoten für Entity

7 Knoten für Verarbeitungsanweisung

8 Knoten für Kommentar

9 Dokument-Knoten

10 Dokumenttyp-Knoten

11 Dokumentfragment-Knoten

12 Knoten für Notation



*/

if (n.nodeType == 3 /*Node.TEXT_NODE*/)

{

var alter_text = n.data;

var neuer_text = replaceSubstring(alter_text,from,to);

var newNode = document.createTextNode(neuer_text);

var parent = n.parentNode;

parent.replaceChild(newNode, n);

}

else

{

var kids = n.childNodes;



for(var i = 0; i < kids.length; i++)

{

replace(kids[i],from,to);

}

}

}



//-->

</script>

</head>

<body>

<p id="p1">This <i>is</i> paragraph 1.</p>

<p id="p2">This <i>is</i> paragraph 2.</p>

<button onclick="replace(document.getElementsByTagName('body')[0],'para','neuerText');">Click Me</button>

</body>

</html>





aktuelle Winamp Playlist                Hörgewohnheiten (Last.fm)



geschrieben von

Login

E-Mail:
  

Passwort:
  

Beitrag anfügen

Symbol:
 
 
 
 
 
 
 
 
 
 
 
 
 

Überschrift: