@mdui/jq
Version:
拥有和 jQuery 相似 API 的轻量级 JavaScript 工具库
28 lines (27 loc) • 1.02 kB
JavaScript
import { $ } from '../$.js';
import { createElement, appendChild } from '../shared/dom.js';
import { eachArray } from '../shared/helper.js';
import './insertAfter.js';
import './insertBefore.js';
import './map.js';
import './remove.js';
eachArray(['appendTo', 'prependTo'], (name, nameIndex) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
$.fn[name] = function (target) {
const extraChilds = [];
const $target = $(target).map((_, element) => {
const childNodes = element.childNodes;
const childLength = childNodes.length;
if (childLength) {
return childNodes[nameIndex ? 0 : childLength - 1];
}
const child = createElement('div');
appendChild(element, child);
extraChilds.push(child);
return child;
});
const $result = this[nameIndex ? 'insertBefore' : 'insertAfter']($target);
$(extraChilds).remove();
return $result;
};
});