UNPKG

@mdui/jq

Version:

拥有和 jQuery 相似 API 的轻量级 JavaScript 工具库

68 lines (67 loc) 2.03 kB
import { $ } from '../$.js'; import { add } from '../shared/event.js'; import { isString, isObjectLike, returnFalse, eachObject, } from '../shared/helper.js'; import './each.js'; import './off.js'; $.fn.on = function ( // eslint-disable-next-line @typescript-eslint/no-explicit-any types, // eslint-disable-next-line @typescript-eslint/no-explicit-any selector, // eslint-disable-next-line @typescript-eslint/no-explicit-any data, // eslint-disable-next-line @typescript-eslint/no-explicit-any callback, one) { // types 可以是 type/func 对象 if (isObjectLike(types)) { // (types-Object, selector, data) if (!isString(selector)) { // (types-Object, data) data = data || selector; selector = undefined; } eachObject(types, (type, fn) => { // selector 和 data 都可能是 undefined // @ts-ignore this.on(type, selector, data, fn, one); }); return this; } if (data == null && callback == null) { // (types, fn) callback = selector; data = selector = undefined; } else if (callback == null) { if (isString(selector)) { // (types, selector, fn) callback = data; data = undefined; } else { // (types, data, fn) callback = data; data = selector; selector = undefined; } } if (callback === false) { callback = returnFalse; } else if (!callback) { return this; } // $().one() if (one) { // eslint-disable-next-line @typescript-eslint/no-this-alias const _this = this; const origCallback = callback; callback = function (event, ...dataN) { _this.off(event.type, selector, callback); return origCallback.call(this, event, ...dataN); }; } return this.each(function () { add(this, types, callback, data, selector); }); };