<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><title>Beginner's Tutorial (runoob.com)</title><Script>functiondisplaydate () {document.getElementById ("Demo"). InnerHTML=Date (); First get ID is the object of the demo, in the re-assigned to the content of the object}</Script></Head><Body><H1>My first JavaScript program</H1><PID= "Demo">This is a paragraph</P><Buttontype= "button"onclick= "displaydate ()">Show Date</Button></Body></HTML>
<!DOCTYPE HTML><HTML><Head> <MetaCharSet= "Utf-8"> <title>Beginner's Tutorial (runoob.com)</title> </Head><Body> <Script>functionchangeimage () {element=document.getElementById ('MyImage') if(Element.src.match ("Bulbon")//The default src is src= "/images/pic_bulboff", and when clicked, Match () determines if SRC contains a string Bulbon {element.src="/images/pic_bulboff.gif"; } Else{element.src="/images/pic_bulbon.gif"; }}</Script><imgID= "MyImage"onclick= "Changeimage ()"src= "/images/pic_bulboff.gif"width= "+"Height= " the"><P>Tap the light bulb to turn the lamp on or off</P> </Body></HTML>
X=document.getelementbyid ("demo") //Find element x.style.color= "#ff0000"; Change Style
function MyFunction () { var X=document.getelementbyid ("Demo"). Value; Gets the ID of the demo object inside your value if (x== "" | | IsNaN (x)) { alert ("Not a number");} }
Note the Var X=document.getelementbyid ("Demo"). Value; If there is a box, such as the input tag with value
var X=document.getelementbyid ("Demo"). InnerHTML no box, such as <p> tags
var person={firstname: "John", LastName: "Doe", id:5566}; Creating objects
Person.lastname; Two ways to access the object properties person["LastName"];
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Script>functionValidateform () {varx=document.forms["MyForm"]["fname"].value; Determine if the value of the fname in the form existsif(x== NULL ||x== "") {alert ("you need to enter a name. "); return false; }}</Script></Head><Body><formname= "MyForm"Action= "demo_form.php"onsubmit= "return Validateform ()"Method= "POST">Name:<inputtype= "text"name= "FName"><inputtype= "Submit"value= "Submit"></form></Body></HTML>
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"></Head><Body><formAction= "demo_form.php"Method= "POST">//Auto-Verify whether the form is empty or low version browser not supported <inputtype= "text"name= "FName"Required= "Required"> <inputtype= "Submit"value= "Submit"></form><P>Click the Submit button, if the input box is empty, the browser will prompt the error message.</P></Body></HTML>
JavaScript Learning Notes