Jquery. each compatible with IE and firefox

Source: Internet
Author: User
The this pointer in jQuery's each method points to a problem, which is a problem encountered during work yesterday. write the each statement: 1: Java code jQuery (& amp; #39; input [typecheckbox] & amp; #39 ;). each (function () {alert (this. checked + this. treeId); // tre

This pointer in jQuery's each method points to a problem, which was encountered during work yesterday.

JQuery. each statement:
1:
Java code
 
JQuery ('input [type = checkbox] '). each (function (){
 
Alert (this. checked + this. treeId); // treeI is a custom attribute.
});


JQuery ('input [type = checkbox] '). each (function (){

Alert (this. checked + this. treeId); // treeI is a custom attribute.
});


This method works normally in IE, but it is not recognized in firefox. Especially for custom attributes, this pointer has errors in closures.

2:
Java code
JQuery. each (jQuery ('input [type = checkbox] '), function (I, item ){
Alert ("I =" + I + ", item =" + jQuery (item). attr ('custom attribute '));
});

JQuery. each (jQuery ('input [type = checkbox] '), function (I, item ){
Alert ("I =" + I + ", item =" + jQuery (item). attr ('custom attribute '));
});


This will avoid using the this pointer, so it can be compatible with ie and firefox. I represents the first variable, and item represents the object referred to by this variable, which is a dom object.

Therefore, I personally recommend that you use the second method in jQuery's each method in the future.

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.