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.