tags:

views:

220

answers:

3

Say,is the following possible:

textNode.appendChild(elementNode);

elementNode refers to those with nodeType=1

textNode refers to those with nodeType=2

It's not easy to produce.

The reason I ask this is that I find a function that adds a cite link to the end of a quotation:

function displayCitations() {
  var quotes = document.getElementsByTagName("blockquote");
  for (var i=0; i<quotes.length; i++) {
  if (!quotes[i].getAttribute("cite")) continue;
  var url = quotes[i].getAttribute("cite");
  var quoteChildren = quotes[i].getElementsByTagName('*');
  if (quoteChildren.length < 1) continue;
  var elem = quoteChildren[quoteChildren.length - 1];
  var link = document.createElement("a");
  var link_text = document.createTextNode("source");
  link.appendChild(link_text);
  link.setAttribute("href",url);
  var superscript = document.createElement("sup");
  superscript.appendChild(link);
  elem.appendChild(superscript);
  }
}

see the last line "elem.appendChild(superscript);" where elem can be a textNode?

I think the reason it's difficult to prove it because it's hard to get access to a specified textNode.Have anyone any way to achieve that?

+3  A: 

I don't think so; I'm fairly certain that something like

<div>this is some <a href="...">text</a> with an element inside it.</div>

ends up being:

<div>
    <textnode/>
    <a>
        <textnode/>
    </a>
    <textnode/>
</div>

I don't believe textNodes can have children.

If I had to guess, I'd think that the result of adding a child node to a text node would be to add the element to the text node's parent instead, but I've not tested that at all.

technophile
Correct. The element is a sibling of the text nodes, not a child.
GalacticCowboy
then the above code is wrong?it's in the book <Web.Design.with.JavaScript.and.the.Document.Object.Model.2005>
Shore
I added a bit at the end of my answer -- I don't think it's wrong, I think the DOM will do the right thing there.
technophile
You mean appending a child to a textNode is the same as appending it to its parent?
Shore
That would be my *guess*. However, it wouldn't be the first time some code got printed in a book despite being completely wrong. :)
technophile
It's a pity there is no simple way to prove whether it's wrong or right.
Shore
Oh,I've misunderstood the author,there is "quotes[i].getElementsByTagName('*');" which ensured "elem" is an elementNode!
Shore
But this question still deserves a answer,i think:)
Shore
A: 

No. Elements may contain attributes, other elements, or text.

Alex Pavlov
hi,I've updated my question so that you can know why I have this question.
Shore
I see. Text node in your sample is a leaf ("source" text under "a" node). And as I said above text coudn't be a parent for anything.
Alex Pavlov
+2  A: 

No, text nodes are always leafs. Instead you must add new nodes to the text node's parent - making them siblings of the text node.

EDIT

Here's an example where I attempt to add a child to a text node.

<div id="test">my only child is this text node</div>

<script type="text/javascript">

var div = document.getElementById( 'test' );
var textNode = div.childNodes[0];
var superscript = document.createElement("sup");
superscript.text = 'test';

textNode.appendChild( superscript );

</script>

Firefox gives the error

uncaught exception: Node cannot be inserted at the specified point in the hierarchy (NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)

Peter Bailey
The conclusion conflicts with code in <Web.Design.with.JavaScript.and.the.Document.Object.Model.2005>
Shore
I'm not sure what you're referencing in your comment, but I added a snippet to backup my statement.
Peter Bailey
thanks a lot,Peter:)
Shore
BTW,what does IE report?
Shore
Well, IE's javascript errors are rarely useful, and this case is no exception. "Unexpected call to method or property access". It does report the correct line number, though.
Peter Bailey