In jquery, you can use the css() method to change the value of the element margin. This method is used to return or set the css attribute value of the element. The margin attribute is used to set the css attribute of the element's outer margin. The syntax is " $("Specified element").css("margin","Modified margin value");".
The operating environment of this tutorial: windows10 system, jquery3.4.1 version, Dell G3 computer.
margin clears the surrounding (outer border) element area. The margin has no background color and is completely transparent.
margin You can change the top, bottom, left, and right margins of the element individually, or you can change all attributes at once.
Possible values
auto Sets the browser margins.
length defines a fixed margin (using pixels, pt, em, etc.)
% defines a margin using percentages
css() method sets or returns one or more style properties of the selected element.
To set the specified CSS property, please use the following syntax:
css("propertyname","value");
The example is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="js/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").css("margin","0px"); }); }); </script> <style> p { background-color:yellow; } p.margin { margin:100px 50px; } </style> </head> <body> <p>这是一个没有指定边距大小的段落。</p> <p class="margin">这是一个指定边距大小的段落。</p> <button>设置 p 元素的margin </button> </body> </html>
Output result:
Video tutorial recommendation: jQuery video tutorial
The above is the detailed content of How to change the margin value in jquery. For more information, please follow other related articles on the PHP Chinese website!