Post Code First
1 <formAction="">2 <P>3 <label for= "X">Number:</label>4 <inputID= "X"name= "X"type= "Number" />5 </P>6 <inputID= "Submit"type= "Submit"value= "Calculat Square" />7 <Script>8 varx=document.getElementById ('x');9 varOutput=Document.createelement ('P');Ten output.textcontent= 'Type a number; it 'll be squared right then!'; One A x.form.appendchild (output); - X.form.onsubmit= function(){return false;}; - X.oninput= function(){ the varv=X.valueasnumber; - output.textcontent=v+ 'Squared is' +v*v; - }; - + varSubmit=document.getElementById ('Submit'); - Submit.parentNode.removeChild (submit); + </Script> A </form>
This code is the square, JavaScript function when the JS calculation directly displayed in the page, the page is forbidden to load JS submitted via submit (refer to NoScript). Here only the base JS is recorded.
Line 9th, thecreateelement () method creates an element node, usually adding text, so the next step is to assign a value output.textcontent = "; then insert the display x.form.appendchild (output); Baidu when you see another way to put it together.
1 var btn=document.createelement ("button"); Create element node 2var t=document.createtextnode ("click ME"); Create a text node 3 btn.appendchild (t); Add a text node to an element
Line 15th, Valueasnumber is HTML5 new, can read the value inside the element, but to cooperate with <input type= "number"/> use, otherwise it will be reported Nan error.
JavaScript Basics-"Web front end Best Practices"