Home > Web Front-end > JS Tutorial > Summary of common javascript methods_javascript skills

Summary of common javascript methods_javascript skills

WBOY
Release: 2016-05-16 15:59:13
Original
1072 people have browsed it

1. JavaScript: Write HTML output

Copy code The code is as follows:

document.write("

This is a heading

");
document.write("

This is a paragraph

");

2. JavaScript: react to events

Copy code The code is as follows:


3. JavaScript: Change HTML content

Copy code The code is as follows:

x=document.getElementById("demo") //Find element
x.innerHTML="Hello JavaScript"; //Change content

4. JavaScript: Change HTML images

Copy code The code is as follows:

element=document.getElementById('myimage')
element.src="../i/eg_bulboff.gif";

5. Change HTML style

Copy code The code is as follows:

x=document.getElementById("demo") //Find element
x.style.color="#ff0000"; //Change style

6. JavaScript is case-sensitive.

JavaScript is case-sensitive.
When writing JavaScript statements, be sure to turn off the case switch.
The functions getElementById and getElementbyID are different.
Likewise, the variable myVariable is different from MyVariable.

7. Tip: A good programming habit is to declare the required variables at the beginning of the code.

8. Value = undefined

In computer programs, variables with no value are often declared. The value of a variable declared without a value is actually undefined. After executing the following statement, the value of the variable carname will be undefined:
var carname;

9. Create JavaScript objects
This example creates an object named "person" and adds four properties to it:

Copy code The code is as follows:

person=new Object();
person.firstname="Bill";
person.lastname="Gates";
person.age=56;
person.eyecolor="blue";

10. JavaScript form verification

Required (or required) items
The following function is used to check whether the user has filled in the required (or required) items in the form. If required or the required field is empty, a warning box will pop 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):

<html>
<head>
<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>
</head>

<body>
<form action="submitpage.htm" onsubmit="return validate_form(this)" method="post">
Email: <input type="text" name="email" size="30">
<input type="submit" value="Submit"> 
</form>
</body>

</html>
Copy after login

The above is the entire content of this article, I hope you all like it

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template