Javascript/jquery dynamically change an element CSS style

Source: Internet
Author: User

JS operation is very simple direct document.getElementById (ID) can be used to operate the

Cases

The code is as follows Copy Code

<! DOCTYPE html>
<title>TITLE</title>
<style type= "Text/css" >
#myDiv {
Background-color:blue;
width:100px;
height:200px;
}
</style>
<script type= "Text/javascript" >
Window.onload = function () {
Alert (document.getElementById ("mydiv"). Style.width);
document.getElementById ("Mydiv"). Style.width = "100px";
}
</script>
<body>
<div id= "mydiv" style= "background-color:red;border:1px solid black" >
Mydiv
</div>
</body>

jquery Operation method

The code is as follows Copy Code
<p class= "MyClass" title= "Choose the fruit you like" > What is your favorite fruit? <p>

In the code above, class is also the attribute of the P element, so getting class and setting class can be done using the attr () method
The code is as follows:

The code is as follows Copy Code
Var p_class=$ ("P"). attr ("class"); Gets the class of the P element

You can also use the attr () method to set class

The code is as follows Copy Code
$ ("P"). attr ("Class", "high"); Set the P element's class to high

In most cases, it replaces the original class with the new class, rather than appending the new class to the original.

The new code is

The code is as follows Copy Code
<p class= "High" title= "Choose the fruit you like" > What is your favorite fruit? <p>

Append style

The code is as follows Copy Code

<style>

. another{

Font-style:italic; /* Italic * *

Color:blue/////* Font set to Blue * *

</style>

Append a style to a Web page

The code is as follows Copy Code

$ ("Input:eq (2)"). Click (function () {

$ ("P"). AddClass ("another");

})

The remove style is done through the Removeclass () method

The code is as follows Copy Code

$ ("P"). Removeclass ("High");

<p class= "High Another" >TEST<P>

To remove P two classes

The code is as follows Copy Code
$ ("P"). Removeclass ("High"). Removeclass ("another");
Or
$ ("P"). Removeclass ("high another");
or remove all classes
$ ("P"). Removeclass ();

Toggle Style
jquery provides Toggleclass () method to control style switching

The code is as follows Copy Code
$ ("P"). Toggleclass ("another");

Determines whether a style is included and returns False if there is a return true

The code is as follows Copy Code

$ ("P"). Hasclass ("another"); Equivalent to $ ("P"). Is (". another");

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.