Javascript Dynamic Setting style instance analysis

Source: Internet
Author: User

Javascript Dynamic Setting style instance analysis

This article describes how to dynamically set styles in javascript. Share it with you for your reference. The specific analysis is as follows:

Dynamic style Modification

1. Easy to error: modifying the style of an element is not a class attribute, but a className attribute.

2. Easy to troubleshoot: Use "style. attribute name" to modify the style attributes separately. Note that the attribute name in css is in javascript.

During the operation, the attribute names may be different, mainly in those attributes that contain-, because

In javascript-it cannot be an attribute or a class name. In CSS, the background color is background-clolor, while in javascript, the background color is style. background; the element style name is class, which is the className attribute in javascript; font-size-> style. fontSize; margin-top-> style. marginTop

3. Modify the style of the Control <input type = "button" value = "AAA" onclick = "this. style. color = 'red'"/>

1. Example 1

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Title> dynamically modify a style </title>

<Style type = "text/css">

. Day

{

Background-color: Green;

}

. Night

{

Background-color: Black;

}

</Style>

<Script type = "text/javascript">

Function dayclick (){

Var divMain = document. getElementById ("divMain ");

// Note that className is used instead of class.

DivMain. className = "day ";

}

Function nightclick (){

Var divMain = document. getElementById ("divMain ");

DivMain. className = "night ";

}

</Script>

</Head>

<Body>

<Div id = "divMain" class = "day">

<Font color = "red"> People's Republic of China </font>

</Div>

<Input type = "button" value = "Daytime" onclick = "dayclick ()"/>

<Input type = "button" value = "" onclick = "nightclick ()"/>

</Body>

</Html>

2. Example: dynamically modify the style (simulate opening and turning off the light)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Title> </title>

<Style type = "text/css">

. Day

{

Background-color: White;

}

. Night

{

Background-color: Black;

}

</Style>

<Script type = "text/javascript">

Function switchLight (){

Var btnSwitch = document. getElementById ("btnSwitch ");

If (document. body. className = "day "){

Document. body. className = "night ";

BtnSwitch. value = "turn on the light ";

}

Else {

Document. body. className = "day ";

BtnSwitch. value = "turn off the light ";

}

}

</Script>

</Head>

<Body class = "night">

<Input type = "button" value = "turn on" id = "btnSwitch" onclick = "switchLight ()"/>

</Body>

</Html>

3. Example:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Title> dynamically set the style exercise (modify the background color of the text box) </title>

<Script type = "text/javascript">

Function IniEvents (){

Var inputs = document. getElementsByTagName ("input ");

For (var I = 0; I <inputs. length; I ++ ){

If (inputs [I]. type = "text "){

// Set the txtOnBlur function as the onblur Event Response Function of the input element

Inputs [I]. onblur = txtOnBlur;

}

}

}

Function txtOnBlur (){

/*

TxtOnBlur is the onblur response function, not called by the response function.

All the objects that can use this to obtain the event control.

*/

If (this. value. length <= 0 ){

This. style. background = "red ";

}

Else {

This. style. background = "white ";

}

}

</Script>

</Head>

<Body onload = "IniEvents ()">

<Input type = "text"/> <br/>

<Input type = "text"/> <br/>

<Input type = "text"/> <br/>

<Input type = "text"/> <br/>

<Input type = "text"/> <br/>

<Input type = "text"/> <br/>

</Body>

</Html>

I hope this article will help you design javascript programs.

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.