CodeWeblog.com » lt,gt 5,several times » The first day of Javascript

The first day of Javascript

container.appendChild (mes);
document.body.appendChild (container);
var newpara = container.cloneNode (true); / / true and false distinction
document.body.appendChild (newpara);

var newpara = container.cloneNode (false); / / true and false distinction
document.body.appendChild (newpara);
</ SCRIPT>
Firebug facie result:

See the difference between true and false instead.
true, then: Yes <p> hello world </ p> cloning.
false: Cloning only <p> </ p>, the text of which is not cloned.
And createElement (), the cloning of the new node will not be automatically inserted into the document. Need to appendChild ();
Another Note: If, after cloning, id like, do not forget to use setAttribute ( "id", "new_id");
To change the new node ID.

4, insert the node. appendChild ():
Used several times in front of all, we should all know about the usage of the.
Specific explanation is that:
Additional elements to a subset of nodes, the new node inserted into the final.
var container = document.createElement ( "p");
var t = document.createTextNode ( "cssrain");
container.appendChild (t);
document.body.appendChild (container);

In addition appendChild () can be used not only new element added, you can also move elements of the existing document.
See the following examples:
<p> msg </ p>
<p> content </ p>
<p> aaaaaaaa </ p>
<script>
var mes = document.getElementById ( "msg");
var container = document.getElementById ( "content");
container.appendChild (mes);
</ script>
/ / Find msg behind on content.
Js internal treatment:
ID for the first msg to delete from the document, and then inserted into the content after the content as the last inserted node.
Results as follows:
<p>
content
<p> msg </ p>
</ p>
<p> aaaaaaaa </ p>

5, insert the node. insertBefore ():
As the name suggests, is to insert a new node to the target in front of the node.
Element.insertBefore (newNode, targerNode);
/ / Note the first parameter is the new node, followed by the target node (insert location).
/ / New node is the guest of his first serve pyronaridine sure. . . ^ _ ^

The second parameter is optional, if not to write the second parameter will be added to the default of the final document, which is equivalent to appendChild ();
Let us look at the insertBefore () how to use:
<div>
<p> 1111 </ p>
<div> msg <div> test </ div> </ div>
<p> 222 </ p>
<p> aaaaaaaa </ p>
</ div>
<script>
var msg = document.getElementById ( "msg");
var aaa = document.getElementById ( "aaa");
var test = document.getElementById ( "cssrian");
test.insertBefore (msg, aaa);
</ script>
/ / We found the insertion of ID for the msg to the front of aaa.
Js-house approach with appendChild () is similar. Is:
ID for the first msg to delete from the document, and then inserted into pre-aaa, aaa, as inserted in front of a node.
Digg Technorati StumbleUpon Mixx del.icio.us Reddit BlinkList Furl YahooMyWeb feedburner

Tags: lt (RSS), gt 5 (RSS), several times (RSS), firebug (RSS), target (RSS), elements (RSS), new id (RSS), document body (RSS), distinction (RSS), subset (RSS), new element (RSS), container document (RSS), node results (RSS), node id (RSS), aaaaaaaa (RSS), insertbefore (RSS)

Permalink: http://www.codeweblog.com/the-first-day-of-javascript/

Leave a reply