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

Summary of common javascript methods_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 15:59:131093browse

1. JavaScript: Write HTML output

Copy code The code is as follows:

document.write("4a249f0d628e2318394fd9b75b4636b1This is a heading473f0a7621bec819994bb5020d29372a");
document.write("e388a4556c0f65e1904146cc1a846beeThis is a paragraph94b3e26ee717c64999d7867364b1b4a3");

2. JavaScript: react to events

Copy code The code is as follows:

247ed3441f34b4891317cdcf2165625cClick here65281c5ac262bf6d81768915a4a77ac0

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>

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

Statement:
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