Window.document Object

Source: Internet
Author: User

1. window.document Object

First, find the element:

Docunment.getelementbyid ("id"); Search by ID, up to one;
var A=docunment.getelementbyid ("id"); Place the found element in a variable;
Docunment.getelementsbyname ("name"); Search by name, find the array;
Docunment.getelementsbytagname ("name"); Based on the name of the tag, find out the array;
Docunment.getelementsbyclassname ("name") according to ClassName, find out the array;

Second, the contents of the operation:

1. Non-form elements:

1) Get content:

alert (a.innerhtml); The HTML code and text in the tag get all the contents of the tag.

such as: The body has such a div:

<div id= "Me" ><b> try it </b></div>

In script, get the contents of the DIV using innerHTML:

var a= document.getElementById ("Me");
alert (a.innerhtml);

Results such as:

alert (a.innertext); take only the words inside
alert (a.outerhtml); includes the contents of the label itself (simple to understand)

1) Set content:

a.innerhtml = "<font color= ' red ' >hello world</font>";

If you use the Set content code result, the contents of the div are replaced with the following:

A.innertext will present the things that are assigned.

Empty content: Assign an empty string

2. Form elements:

1) to get the content, there are two ways to get it:

var t = document.f1.t1; The form ID of the form is F1 with ID T1 input;
var t =document.getelementbyid ("id"); Get directly with ID.

alert (T.value); Gets the value in input;
alert (t.innerhtml); Get <textarea> value here </textarea>;

2) Setting content: t.value= "content change";

3. A small Knowledge point:

<a href= "http://www.baidu.com" onclick = "Returnfalse" > Turn to Baidu </a>, add returnflase will not jump, default is return True will jump. The button is the same, if the button is set to return flase will not commit, this can be used to control the commit jump.

Third, Operation Properties

This element is first found using the ID of the element and stored in a variable:

var a = document.getElementById ("id");

You can then manipulate the attributes of the element:

A.setattribute ("attribute name", "attribute value"); Set a property, add or change all can;

A.getattribute ("attribute name"); Gets the value of the property;

A.removeattribute ("attribute name"); Remove an attribute.

Example 1: Do a question, if the input answer is correct then pop up correctly, error pop-up error;

Here in text write a Daan attribute, which stored the value of the answer, click to check the answer when Cheak input and answer whether the same:

Body in code:

<form> which year was the Republic of China founded? <input type= "Text" daan= "1912" value= "id=" T1 "name=" T1 "/><input type=" button "onclick=" Check () "Id=" T2 " Name= "T2" value= "Check Answer"/></form>

The code in JS:

function Check ()
{
var A=document.getelementbyid ("T1");
var A1=a.value;
var a2=a.getattribute ("Daan");
if (A1==A2)
{
Alert ("Congratulations on your answer!") ");
}
Else
{
Alert ("Idiot! ");
}
}

Example 2: The Consent button, countdown 10 seconds, the consent button becomes available, here is the Action property: Disabled, to change the state of the button, when disabled= "disabled" button is not available.

The code in the body:

<form><input type= "Submit" Id= "B1" Name= "B1" value= "agree (9)" disabled= "disabled"/></form>

The code in JS:

var n=10;var a= document.getElementById ("B1"); function Bian ()
{
n--;
if (n==0)
{
A.removeattribute ("Disabled");
a.value= "Consent";
Return
}
Else
{
a.value= "Consent (" +n+ ")";
Window.settimeout ("Bian ()", 1000);
}
}
Window.settimeout ("Bian ()", 1000);

Four, Operation style

This element is first found using the ID of the element and stored in a variable:

var a = document.getElementById ("id");

You can then manipulate the attributes of the element:

A.style= ""; Manipulate the properties of this ID style.

Styles are styles in CSS, and all styles can be manipulated in code.

document.body.style.backgroundcolor= "COLOR"; the background color of the entire window.

Action Style class:a.classname= "className in style sheet" action batch style

Example 1: Automatic and manual switching of the display image;

The code in body, make a div with background picture and control object on both sides:

</div>
<div id= "Tuijian" style= "Background-image:url (imges/tj1.jpg);" >
<div class= "pages" id= "P1" onclick= "Dodo ( -1)" ></div>
<div class= "pages" id= "P2" onclick= "Dodo (1)" ></div>
</div>

Code in the style sheet:

<style type= "Text/css" >
*{
margin:0px Auto;
padding:0px;
Font-family: "Microsoft Jas Black";
}
#tuijian {
width:760px;
height:350px;
Background-repeat:no-repeat;
}
. pages{
top:200px;
Background-color: #000;
Background-position:center;
Background-repeat:no-repeat;
opacity:0.4;
width:30px;
height:60px;
}
#p1 {
Background-image:url (imges/prev.png);
Float:left;
margin:150px 0px 0px 10px;
}
#p2 {
Background-image:url (imges/next.png);
Float:right;
margin:150px 10px 0px 0px;
}
</style>

JS in the code, here is mainly every 3 seconds to call the Huan () function, to the background image of the style modification, when the left and right switch to switch to manual switching, automatic switching stop:

<script language= "JavaScript" >
var jpg =new Array ();
jpg[0]= "url (imges/tj1.jpg)";
jpg[1]= "url (imges/tj2.jpg)";
jpg[2]= "url (imges/tj3.jpg)";
var tjimg = document.getElementById ("Tuijian");
var xb=0;
var n=0;
Function Huan ()
{
xb++;
if (XB = = jpg.length)
{
xb=0;
}
TJIMG.STYLE.BACKGROUNDIMAGE=JPG[XB];
if (n==0)
{
var id = window.settimeout ("Huan ()", 3000);
}
}
Function Dodo (m)
N=1;
XB = xb+m;
if (XB < 0)
{
XB = jpg.length-1;
}
else if (XB >= jpg.length)
{
XB = 0;
}
TJIMG.STYLE.BACKGROUNDIMAGE=JPG[XB];
}
Window.settimeout ("Huan ()", +);</script>

Window.document Object

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.