Difference between jquery. ATTR ('class') and jquery. ATTR ('classname ')

Source: Internet
Author: User
Today, my colleagues thought jquery. ATTR ('class') and jquery. the ATTR ('classname') operation has the question of who is talking about. In fact, the last result of this operation is the same, the function is to obtain or update the class of the HTML element, as shown in the following example:
<div id="div1" class='A B C D'></div>

Once you want to convert all the classes in div1 into E, F, G, and H, you can use jquery. addclass and jquery. removeclass is too tired. Isn't it a good solution at a time, so you can:

$ ('# Div1 '). ATTR ('class', 'e f g H'); // The result is the same $ ('# div1 '). ATTR ('classname', 'e f g H ');

You may wonder why two identical operations have to be implemented in two ways. There is a hidden source.

 

In the HTML Dom, the class is called classname (I don't know why)

Source W3C School

 

So the preceding example is changed to Dom to compile

Document. getelementbyid ('div1 '). classname = 'e f g H' // normal document. getelementbyid ('div1 '). class = 'e f g H' // error message

 

Why does it happen again when jquery is merged?

This is because jquery is used as Callback handler.

 

In such as the beginning of line 967th in the jquery-1.4.1.js, will be set in jquery with the word, what is the actual response of the word?

jQuery.props = {    "for": "htmlFor",    "class": "className",    readonly: "readOnly",    maxlength: "maxLength",    cellspacing: "cellSpacing",    rowspan: "rowSpan",    colspan: "colSpan",    tabindex: "tabIndex",    usemap: "useMap",    frameborder: "frameBorder"};

When jquery calls ATTR, the name will find the corresponding response.

name = notxml && jQuery.props[ name ] || name;

So when the class is merged, the class is actually returned.

 

Both

  • Use class: it is the same as the plain and HTML tag, and the use of class is less than the classname, it may be faster than the second.
  • Use classname: it is the same as HTML Dom. Javascript cannot be used because jquery is not used.

However, it is hard to say who the two are right or who they are. It seems that they have no intention, because they both are right and have their own shortcomings. As long as the members of the case use the same statement, I think so.

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.