JQuery entry: CSS, style, and size

Source: Internet
Author: User

JQuery entry: CSS, style, and size

JQuery provides an easy way to obtain and set the CSS attributes of elements:

// Get the CSS attribute $(h1 ).css (fontSize); // Returns a string such as 19px. Return a string 19px. $(H1 2.16.css (font-size); // Also works. It Also works. // Set the CSS attributes. $(H1 2.16.css (fontSize, 100px); // Setting an individual property. Set individual attributes // set multiple attributes. $(H1 2.16.css ({fontSize: 100px, color: red });

Note the form of the parameter in the second line-it is an object containing multiple attributes. This is a common method to pass multiple parameters to a function. Many jQuery setting methods accept objects that can set multiple values at a time.

CSS attributes in JavaScript usually need to replace the font size with the camper spelling. For example, CSS attributesfont-sizeWhen used as an attribute name in JavascriptfontSize. However.css()When a CSS attribute name is passed like a string, it is not used in this way-in this case, whether it is a camper spelling or a font size, it will run.

When setting CSS with an object, CSS attributes should be spelled in a camper style instead of a font size. However, it is not recommended in production-ready code..css()As the setting function.

Apply CSS classes to set styles

As a function,.css()Methods are very valuable. However, it is often avoided as a setting function in the generated ready code, because the information normally presented is best kept separate from the JavaScript code. Instead, use classes to write CSS rules that describe Multiple display forms, and then change the class of the element.

// Use classes to work. Var h1 = $ (h1); h1.addClass (big); h1.removeClass (big); h1.toggleClass (big); if (h1.hasClass (big )){...}

Classes can also be used to store status information about an element, for example, indicating which element is selected.

Size

JQeury provides a variety of methods to obtain and modify the size and positioning information of an element.

The following code provides a brief overview of the element size acquisition function in jQuery. For details about the element size method in jQuery, see dimensions documentation on api.jquery.com.

// Basic size method. // Set all
The width of the element. $ (H1). width (50px); // obtain the first one The width of the element. $ (H1). width (); // set all The height of the element. $ (H1). height (50px); // obtain the first one The width of the element. $ (H1). height (); // returns an object that contains the first Similar to the positioning information of its "parent element offset (positioning. $ (H1). position ();

 

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.