1. What can JavaScript do?
(1) write HTML outputdocument.write ("
(2) responding to events<button type= "button" onclick= "alert (' welcome! ')" > Click here </button>
(3) changing HTML contentX=document.getelementbyid ("demo"); x.innerhtml= "Hello JavaScript";
(4) Changing the attributes of HTML elements (HTML images)function Changeimage () {Element=document.getelementbyid (' myimage ') if (Element.src.match ("Bulbon")) {element.src= "/ I/eg_bulboff.gif ";} Else{element.src= "/i/eg_bulbon.gif";}}
(5) changing HTML styleX=document.getelementbyid ("demo"); x.style.color= "#ff0000"
(6) Validate input
2. Where JavaScript scripts are loaded
(1) in the <body> and Note that the script in HTML must be between <script> and </script>(2) Save the script to an external fileTo use an external file, set the. js file in the "src" attribute of the <script> tag to not include the <script> tag.3. Note Points for JavaScript statements(1) Case sensitive (2) automatically ignores extra spaces (3) use backslashes in a text string to wrap lines of code (4) Single-line Comments (//) multiline Comments (/* *)4. Note Points for JavaScript variables(1) Case sensitive (2) must start with a letter, but can also start with $ and _ (3) to declare the variable by "var"5. JavaScript data type(1) JavaScript has a dynamic type (2) string: Single or double quotes (3) digits: With decimal point and no decimal points, scientific notation (4) Boolean type: Ture False (5) array: var cars=new array (); (6) object: delimited by curly braces ,。 Inside the parentheses, the properties of the object are defined in the form of name and value pairs (name:value). Attributes are separated by commasobject properties are addressed in two ways:name=person.lastname;name=person["LastName"];6. JavaScript ObjectsA property is a value associated with an object. Method is an action that can be performed on an object.(1) Creating JavaScript objectsAlmost all of the transactions in JavaScript are objects: strings, numbers, arrays, dates, functions, and so on. You can also create your own objects. Person=new Object (); Person.firstname= "Bill"; Person.lastname= "Gates"; person.age=56; Person.eyecolor= "Blue";(2) Accessing the properties of an objectObjectnamepropertynamevar message= "Hello world!"; var x=message.length;(3) Methods of accessing objectsObjectnamemethodnamevar message= "Hello world!"; var x=message.touppercase ();7. JavaScript functions(1) Function syntax
function functionname ()
{
Here is the code to execute
}
(2) function with parameters
function MyFunction (VAR1,VAR2)
{
Here is the code to execute
}
(3) function with return value
function MyFunction ()
{
var x=5;
return x;
}
You can also use the return statement if you just want to exit the function
function MyFunction (A, B)
{
if (a>b)
{
Return
}
X=a+b
}
8. JavaScript operators(1) Arithmetic operator: +-*/% + +-(2) assignment operator: = + = = *=/=%= (3) comparison operator: (4) logical operator (5) conditional operator:
9. JavaScript Judgment Statements
- If statement-use this statement to execute code only if the specified condition is true
- If...else Statement-executes code when the condition is true and executes other code when the condition is false
- If...else If....else Statement-Use this statement to select one of several code blocks to execute
- Switch statement-Use this statement to select one of several code blocks to execute
For/in-Looping through the properties of an object
var person={fname: "John", lname: "Doe", age:25};
for (x in person)
{
Txt=txt + person[x];
}
Running Result: JohnDoe25
JavaScript error-throw, try, and catch
The TRY statement tests the code block for errors.
The catch statement handles the error.
The throw statement creates a custom error.
The
(1) JavaScript test and capture
Try statement allows us to define a block of code that performs error testing at execution time. The
Catch statement allows us to define a block of code that executes when a try block of code has an error. The
JavaScript statement try and catch are paired occurrences. The
(2) throw statement
Throw statement allows us to create custom errors. The
Correct technical term is: Create or throw an exception (exception).
If you use throw with try and catch, you can control the flow and generate custom error messages.
<script>
function myFunction ()
{
Try
{
var X=document.getelementbyid ("Demo"). Value;
if (x== "") throw "empty";
if (IsNaN (x)) throw "not a number";
if (x>10) throw "too high";
if (x<5) throw "too low";
}
catch (Err)
{
var Y=document.getelementbyid ("mess");
y.innerhtml= "Error:" + Err + ".";
}
}
</script>
One. JavaScript form validation
JavaScript can be used to validate these input data in an HTML form before the data is sent to the server.
JavaScript Form Validation
JavaScript can be used to validate these input data in an HTML form before the data is sent to the server.
These typical forms of data that are validated by JavaScript are:
Has the user filled out the required fields in the form?
is the email address entered by the user legal?
Has the user entered a valid date?
Does the user enter text in the Data field (numeric field)?
(1) required (or required) items
<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>
(2) e-mail verification
This means that the data entered must contain the @ symbol and the dot (.). At the same time, @ must not be the first character of the email address, and at least one point after @
<script type= "Text/javascript" >
function Validate_email (field,alerttxt)
{
With (field)
{
apos=value.indexof ("@")
dotpos=value.lastindexof (".")
if (apos<1| | DOTPOS-APOS<2)
{alert (alerttxt); return false}
else {return true}
}
}
function Validate_form (thisform)
{
With (thisform)
{
if (validate_email (email, "Not a valid e-mail address!") ==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>
Description
With (field) {}; setting scope
The focus () method is used to give the element the focal point. This allows the user to edit the displayed text without having to click on it.
"Learning Site" W3school
JavaScript Learning notes-basic knowledge