Use jQuery to manipulate CSS styles of elements (get, modify, etc.)

//1, get and set the style

$("#tow").attr("class") Get the class attribute whose ID is tow

$("#two").attr("class","divClass") sets the class attribute whose Id is two.

//2, additional style

$("#two").addClass("divClass2") adds the style divClass2 to the object whose ID is two

//3, remove the style

$("#two").removeClass("divClass") removes the style with the class name divClass of the object whose ID is two.

$(#two).removeClass("divClass divClass2") removes multiple styles.

//4, switch class name

$("#two").toggleClass("anotherClass") //Repeat switch anotherClass style

//5, determine whether there is a style

$("#two").hasClass("another")==$("#two").is(".another");

//6, get the style in the css style

$("div").css("color") Set the color property value. $(element).css(style)

//set a single style

$("div").css("color","red")

//set multiple styles

$("div").css({fontSize:"30px",color:"red"})

$("div").css("height","30px")==$("div").height("30px")

$("div").css("width","30px")==$("div").height("30px")

//7.offset() method

//Its role is to get the relative offset of the element in the current window, and the returned object contains two properties, namely top and left.

//Note: Only valid for visible elements.

var offset=$("div").offset();

var left=offset.left; //Get the left offset

var top=offset.top; //Get the right offset

//8, position() method

//Its role is to get the relative offset of the element relative to the nearest grandparent node whose position style attribute is set to relative or absolute. Like offset(), the object it returns also includes two attributes, top and left.

//9, scrollTop() method and scrollLeft() method

$("div").scrollTop(); //Get the distance of the scrollbar of the element from the top.

$("div").scrollLeft(); //Get the distance of the scroll bar of the element from the left.

//10. The toggle and slideToggle methods in jQuery can both display and hide an element. The difference is:

//toggle: The dynamic effect is from right to left. Lateral action.

//slideToggle: The dynamic effect is from bottom to top. Vertical action.

//For example, if you want to achieve a dynamic effect of tree shrinking from bottom to top, you can use slideToggle.

$('input').attr("readonly",true)//将input元素设置为readonly
$('input').attr("readonly",false)//去除input元素的readonly属性
$('input').attr("disabled",true)//将input元素设置为disabled
$('input').attr("disabled",false)//去除input元素的disabled属性

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325476584&siteId=291194637