Creating elements

Elements created in three ways

In order to improve the user experience 

1. document.write ( "tag code and a content");

  Create a page load, there is no problem: document.write ( "<p> This is a p </ p>");

  Defects: If after the page is loaded, this method creates all content present on the page are gone 

$ My ( "BTN") the onclick =. function () { 

    document.write ( "<P> This is a P </ P>" ); 

  };

2. .innerHTML parent objects ( "tag and the content of the code");

  $ My ( "BTN"). the onclick = function () { 
    My $ ( "DV"). the innerHTML = "<P> window moonlight, suspected ground frost, for the first moon, down and think home </ p> " ; 
  };

3. document.createElement ( "name tags");

The first step: document.createElement ( "name tags");

Step Two: Append: .appendChild parent element (child element objects);

    Insert: parent element .inerstBefore (new child objects, child objects referenced);

    Remove: parent element .removeChild (to get rid of the child element of an object);

// click of a button, create a div p in the 
  My $ ( "btn") onclick =. Function () {
     // create the element 
    var pObj = document.createElement ( "p" ); 
    setInnnerText (pObj, "This is a P " );
     // the sub-element is added to create the parent element 
    My $ (" DV " ) .appendChild (pObj); 
  };

 

Guess you like

Origin www.cnblogs.com/zhangDY/p/11494779.html