Jo: Finde den Fehler nicht

Hallo.

function addElem(e) {  
	mainForm = document.getElementById("Mediacontainer");  
	if(e=='t'){  
		var newBlock="text";  
	}  
	if(e=='b'){  
		var newBlock="bild";  
	}  
	var newnew=document.createTextNode(newBlock);  
	mainForm.lastChild.appendChild(newnew.nodeValue);  
}

Es soll wenn addElem('t') ausgeführt wird, "text" hinten am DIV mit der ID "Mediacontainer" drangehängt werden, bei 'b' soll es "bild" sein.

Es geht nicht.

Fehlerkonsole sagt:

Fehler: uncaught exception: [Exception... "Could not convert JavaScript argument arg 0"  nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)"  location: "JS frame :: skript.js :: addElem :: line 127"  data: no]

Und line 127 ist: mainForm.lastChild.appendChild(newnew.nodeValue);

Also woran liegts??
Ich komm nicht weiter.

Gruß, Jo

  1. Hi,

    var newnew=document.createTextNode(newBlock);
    mainForm.lastChild.appendChild(newnew.nodeValue);

    appendChild hängt Node-Objekte ins Dokument ein, nicht irgendwelche *Eigenschaften* von Nodes.

    MfG ChrisB

    --
    Light travels faster than sound - that's why most people appear bright until you hear them speak.
    1. Hi.

      Sprich es müsste so aussehen:

      var newnew=document.createTextNode(newBlock);  
      mainForm.lastChild.appendChild(newnew);
      

      Dann kommt aber folgender Fehler:
      Fehler: uncaught exception: [Exception... "Node cannot be inserted at the specified point in the hierarchy"  code: "3" nsresult: "0x80530003 (NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)"  location: "http://localhost/Scripts/XMLHttpRequest.js Line: 127"]

      =/...

      1. Sprich es müsste so aussehen:

        var newnew=document.createTextNode(newBlock);

        mainForm.lastChild.appendChild(newnew);

        
        >   
        >   
        > Dann kommt aber folgender Fehler:  
        > Fehler: uncaught exception: [Exception... "Node cannot be inserted at the specified point in the hierarchy"  code: "3" nsresult: "0x80530003 (NS\_ERROR\_DOM\_HIERARCHY\_REQUEST\_ERR)"  location: "http://localhost/Scripts/XMLHttpRequest.js Line: 127"]  
          
        Was ist lastChild für ein Objekt?  
          
        Struppi.
        
        1. Hi

          Was ist lastChild für ein Objekt?

          <ul></ul>

          1. Hi

            Was ist lastChild für ein Objekt?

            <ul></ul>

            Woher weißt du das?

            Struppi.

          2. Hi,

            Was ist lastChild für ein Objekt?

            <ul></ul>

            Und wieso willst du in eine Liste einen TextNode einhängen?

            Das sollte zwar gehen, so lange es sich um reinen Whitespace handelt (wäre dann aber nicht sonderlich sinnvoll).
            "Richtiger" Text hat als Kindelement in einer UL jedenfalls nichts verloren - sondern erst in einem darunter hängenden LI.

            MfG ChrisB

            --
            Light travels faster than sound - that's why most people appear bright until you hear them speak.