Jquery 对象集合的迭代扩展forEach

时间:2022-12-03 00:12:05
 if (jQuery && !jQuery.fn.forEach) {
$(function () {
(function ($) {
$.fn.extend({
forEach: function (predicate) { if (this == null) {
throw new TypeError(' this is null or not defined');
} // 1. Let O be the result of calling toObject() passing the
// |this| value as the argument.
var O = Object(this); // 2. If isCallable(predicate) is false, throw a TypeError exception.
if (typeof predicate !== "function") {
throw new TypeError(predicate + ' is not a function');
} //3 call the jq original API for iteror
$.each(O, function (index, domEle) {
predicate($(domEle));
});
}
})
})(jQuery); });
}