Attributes and style settings in jquery

Source: Internet
Author: User

Add attribute

$target.attr({"title":"one piece","name":"solgan"});

Add the title and name attributes to the target Element

Before adding:

I am Lu Fei, the man who wants to become the pirate king.

After adding:

I am Lu Fei, the man who wants to become the pirate king.


Remove attribute

 $target.removeAttr("title");

Remove an existing style. If the style does not exist, no error is reported.

Before removing a style:

I am Lu Fei, the man who wants to become the pirate king.


After the style is removed:

I am Lu Fei, the man who wants to become the pirate king.


Set Style

 $target.attr("class","red");

The so-called set style is actually to modify the class attribute of the target element. Note that this is to change the class to red instead of adding it on the basis of the original class.

Before setting a style:

I am Lu Fei, the man who wants to become the pirate king.


After setting the style:

I am Lu Fei, the man who wants to become the pirate king.


Add Style

 $target.addClass("bg");

Add a style based on the original class, instead of replacing

Before adding a style:

I am Lu Fei, the man who wants to become the pirate king.

After adding a style:

I am Lu Fei, the man who wants to become the pirate king.



Delete A style

$target.removeClass("bg");

If the style exists, delete it. If it does not exist, no error is reported.

Before deleting a style:

I am Lu Fei, the man who wants to become the pirate king.

After the style is deleted:

I am Lu Fei, the man who wants to become the pirate king.


Determine whether a style exists

$target.hasClass("bg");

If a style exists, true is returned; otherwise, false is returned.



Add css styles

$target.css("text-decoration","underline");

Before adding a css style:

I am Lu Fei, the man who wants to become the pirate king.


After adding the css style:

I am Lu Fei, the man who wants to become the pirate king.




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.