jquery operation properties and style explained

Source: Internet
Author: User

We can use GetAttribute and setattribute in JavaScript to manipulate element attributes of elements. In JQuery, you are provided with a attr () wrapper set function that allows you to manipulate the attributes of all the elements in the wrapper set:

Although we can use REMOVEATTR (name) to delete element properties, the corresponding DOM attribute is not deleted, only the value of the DOM property is affected. For example, removing the readonly element attribute of an INPUT element causes the corresponding DOM property to become False ( That is, input becomes an editable state).

? modifying CSS classes and styles

The following table is a way to modify the CSS class-related JQuery method:

<!doctype html> "zh"> <meata charset="utf-8"/> <title>get object width</title> <script src="jquery-1.11.2.min.js"></script> <script>  $(function() {   alert("attr(\"width\"):" + $("#testDiv").attr("width")); //undifined   alert("css(\"width\"):" + $("#testDiv").css("width")); //auto(ie6) 或 1264px(ff)   alert("width():" + $("#testDiv").width()); //正确的数值 1264   alert("style.width:" + $("#testDiv")[0].style.width); //空值  }) </script><body> <div id="testDiv">test text</div></body>

We want to get the width of the test layer and use the attr method to get "element attribute" to undefined because it does not add width to the div. While using the CSS () method, although the value of the style property can be obtained, but the results returned in different browsers are different, IE6 returns auto, while the FF returns the correct value but with "px" behind it. So jQuery provides the width () method, which returns the correct value without PX.

For the above question, JQuery provides a way to get and set the common properties, such as width () the user gets the widths of the elements, and width (val) is used to set the element width.

Outerwidth can accept a bool value parameter to indicate whether the margin value is calculated.

2. Location-related positioning

jquery operation properties and style explained

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.