1. JavaScript: Writing HTML output
document.write ("document.write ("<p>this is a paragraph</p>");
2. JavaScript: Responding to events
<button type= "button" onclick= "alert (' welcome! ')" > Click here </button>
3. JavaScript: Changing HTML content
X=document.getelementbyid ("demo")//Find elements
X.innerhtml= "Hello JavaScript"; Change Content
4. JavaScript: changing HTML images
Element=document.getelementbyid (' MyImage ')
Element.src= ". /i/eg_bulboff.gif ";
5. Change HTML Style
X=document.getelementbyid ("demo")//Find element
X.style.color= "#ff0000"; Change Style
6. JavaScript is sensitive to case.
JavaScript is sensitive to capitalization.
When writing JavaScript statements, be aware of whether to turn off the case toggle key.
The function getElementById is different from the getElementById.
Similarly, variable myvariable and myvariable are also different.
7, Hint: a good programming habit is, at the beginning of the code, the unified declaration of the required variables.
8. Value = undefined
In computer programs, variables that are not valued are often declared. A variable that is not declared with a value, whose value is actually undefined. After the following statement has been executed, the value of the variable carname will be undefined:
var carname;
8. Creating JavaScript Objects
This example creates an object named "Person" and adds four properties to it:
Person=new Object ();
Person.firstname= "Bill";
Person.lastname= "Gates";
person.age=56;
Person.eyecolor= "Blue";
9. JavaScript form Verification
Required (or required) items
The following function is used to check whether the user has filled out the required (or required) items in the form. If the required or required option is empty, the warning box pops up, and the return value of the function is false, otherwise the return value of the function is true (meaning there is no problem with the data):
<script type= "Text/javascript" >
function validate_required (field,alerttxt)
{
With (field)
{
if (value==null| | value== "")
{alert (alerttxt); return false}
else {return true}
}
}
function Validate_form (thisform)
{
With (Thisform)
{
if (validate_required email, "Email must be filled out!") ==false)
{Email.focus (); return false}
}
}
</script>
<body>
<form action= "submitpage.htm" onsubmit= "return Validate_form (This)" method= "POST" >
Email: <input type= "text" name= "email" size= ">"
<input type= "Submit" value= "Submit" >
</form>
</body>
JavaScript Learning Diary 1