jquery the fastest way to set CSS styles

Source: Internet
Author: User

The test environment is Firefox, IE is not measured.
Directly gives the result: the fastest is the function setcss (els,name,val) {} This method of setting style, the second is to change the class name, the slowest is CSS ()

Recommended simple operation style using the method, a large number of CSS, and to facilitate the user to make their own skin, it should be better to change the class name.

The code is as follows Copy Code

Console.time ("Change class name");

$ ("P"). addclass ("a");
Console.timeend ("Change class name");

Console.time ("Use CSS ()");
$ ("P"). CSS ("Color", "red");
Console.timeend ("Use CSS ()");

Console.time (' style settings converted to JS ');
/*var p = $ ("P");
var len = p.length;
for (Var i=0;i<len;i++) {
P[i].style.display = "block";
}*/
Setcss ("Li", "display", "none");
function Setcss (els,name,val) {
var obj = $ (eLs);
var len = obj.length;
for (Var i=0;i<len;i++) {
Obj[i].style[name] = val;
}
}

Console.timeend (' style settings converted to JS ');
Console.time (' Convert to JS style setting 2 ');
/*var p = $ ("P");
var len = p.length;
for (Var i=0;i<len;i++) {
P[i].style.display = "block";
}*/
Setcss ("Li", "display", "block");
function Setcss (els,name,val) {
var obj = $ (eLs);
var len = obj.length;
for (Var i=0;i<len;i++) {
Obj[i].style[name] = val;
}
}
Console.timeend (' Convert to JS style setting 2 ');

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.