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"); |