Window.document Objects (2)

Source: Internet
Author: User
Tags mathematical functions

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 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;
}
#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>


Five, related elements operation:

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

var B = a.nextsibling, find the next sibling element of a, note the inclusion of spaces;

var B = a.previoussibling, find the previous sibling element of a, note the inclusion of spaces;

var B = A.parentnode, find the upper-level parent element of A;

var B = a.childnodes, find the array, find the next level of a sub-element;

var B = a.firstchild, first child element, LastChild last, Childnodes[n] find a few;

Alert (Nodes[i] instanceof Text); Judging is not the text, is returning true, not returning flase, using if to determine if its value is false, you can remove the space.

Vi. creation, addition and deletion of elements:

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

var obj = document.createelement ("tag name"); Create an element

obj.innerhtml = "Hello World"; When you add it, you first need to create an element.

A.appendchild (obj); Adds a child element to a.

A.removechild (obj); Delete a child element.

List of A.selectedindex: Selected is the first;

A.options[a.selectindex] Press the subscript to remove the first option object

Seven, the operation of the string:

var s = new String (); or var s = "AAAA";

var s = "Hello World";

Alert (S.tolowercase ()); turn lowercase touppercase () to uppercase

Alert (s.substring (3,8)); Intercept to eighth position from the third position

Alert (S.SUBSTR (3,8)), starting from the third position intercept, intercept eight characters length, do not write the following number is truncated to the last.

S.split ("); Replace the character with the specified character, put in an array, automatically sort

S.length is a property

S.indexof ("World"), where the first occurrence of world in a string is not returned-1

S.lastindexof ("O"); o the last occurrence in the string

Eight, Date-time Operation

var d = new Date (); Current time

D.setfullyear (2015,11,6);/* minus 1 settings on the month you want to set */

D.getfullyear: Take the year;

D.getmonth (): Take the month, take out the less 1;

D.getdate (): Take the day;

D.getday (): Pick up the day of the week

D.gethours (): Take the hour;

D.getminutes (): Take minutes; D.getseconds (): Take seconds

D.setfullyear (): Set the year, note-1 when setting the month.

Nine, operation of Mathematical functions

Math.ceil (); the smallest integer greater than the current decimal

Math.floor (); The largest integer of the current decimal number of a small fish

MATH.SQRT (); open square

Math.Round (); rounding

Math.random (); random number, 0-1

Ten, small knowledge points

Outside double quotes, inside the double quotation marks to single quotation marks;

When the row height setting is set in the Div, the row occupied by default is in the middle position (middle of the div in the upper and lower section-"default" vertically centered), no matter how high the setting is.

The value of the text box is a string that needs to be converted to a number using parseint ().

S.match (REG); s represents a string, and Reg represents a string that matches both, and returns a null if two strings do not match.

Third, document:
1. Find
var d = document.getElementById ("ID of element");
var d = document.getelementsbyname ("element name")
var d = document.getelementsbytagname ("label name")

2. Operating element content:
(1) Form element: Text (Input:type=text,input:type=password;textarea;input:type=hidden) button (submit,reset,buttonimage) selection (radio , Checkbox,select,file)
d.value=xxxx;
var s = d.value;

(2) Non-form elements: H1...h6,p,div,ul,ol,li
d.innerhtml = xxxx;
var s = d.innerhtml;

3. Manipulating element properties:
D.setattribute (name, value)
var s = d.getattribute (name)
D.removeattribute (name);

4. Operation element Style:
(1) Inline style:
D.style.backgroundcolor = "#FFFF00";
var s = d.style.backgroundcolor;
(2) Class attribute:
D.classname= ""
5. Operation Related elements:
Father, son, brother, brother

Window.document Objects (2)

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.