JavaScript Basics-"Web front end Best Practices"

Source: Internet
Author: User

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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.