Creates a new element node and adds the node as a child of the specified node in the document
1, a new element node is created, and the return value is a reference to the element node
var liNode =document.createlement ("Li");
var Citynode=document.getelementbyid ("City");
The new Newchild child node is added as a elementnode
cityNode.appendChild(liNode);
2, create a text node Creattextnode
var xmText=document.creatTextNode("厦门");
Requirements: Click the Submit button, check whether to select type, if there is no choice to give a hint: "Please choose the type";
Check the text box for input (you can remove before and after spaces), if not input, give a hint: "Please enter the content";
If the inspection is passed, then add the corresponding Li node//requirement 2 in the corresponding UL node: Enable the addition of Li to respond to the OnClick event: The text value of the pop-up li. Window.onload = function () {function showcontent (liNode) {alert ("^_^ #" + liNode.firstChild.nodeValue);} var linodes = doc
Ument.getelementsbytagname ("Li"); for (var i = 0, i < linodes.length i++) {Linodes[i].onclick = function () {showcontent (this);}}//1.
Get #submit corresponding button submitbtn var submit = document.getElementById ("submit"); 2. Add the onclick response function to submitbtn = Submit.onclick = function () {//4. Check to see if Type is selected, if no selection is given: "Select Type"//4.1 Select all nodes of name= "type"
Types var types = document.getelementsbyname ("type");
4.2 Traverse types to check if it has a type checked attribute exists to indicate that//a type is selected: Determines whether an element node has//This property by using the if (element node. Attribute name).
var typeval = null; for (var i = 0; i < types.length. i++) {if (types[i].checked) {typeval = Types[i].value; break;}}//4.3 If none of the type is selected , then eject: "Select Type." Response method End://return False if (TypevaL = = null) {alert ("Please select Type"); return false;}//5.
Gets the literal value of name= "name": Through the Value property: nameval var nameele = document.getelementsbyname ("name") [0];
var nameval = Nameele.value; 6.
Remove the front and back spaces of the nameval.
var reg =/^\s*|\s*$/g;
Nameval = Nameval.replace (Reg, "");
The text box for name also removes the front and back spaces.
Nameele.value = Nameval; 6.
Compare Nameval and "", If "" "" "" "" "Please enter the content/method end: Return False if (Nameval = =" ") {alert (" Please enter content ");
return false; }//7.
Create Li node var liNode = document.createelement ("Li"); 8.
Use Nameval to create a text node var content = document.createTextNode (nameval); 9.
Linode.appendchild (content) with 8 plus 7 sub nodes; 11. Add the onclick response function to the newly created Li Linode.onclick = function () {showcontent (this);}//10.
Add 7 to the selected type corresponding to the UL sub node document.getElementById (typeval). appendchild (LiNode); 3.
By adding return false at the end of the onclick response function, you can cancel the//default behavior of the Submit button.
return false; } </script>
Above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!