@whitesev/domutils 1.3.4 → 1.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.amd.js CHANGED
@@ -595,11 +595,18 @@ define((function () { 'use strict';
595
595
  click(element, handler, details, useDispatchToTriggerEvent) {
596
596
  let DOMUtilsContext = this;
597
597
  if (typeof element === "string") {
598
- element = DOMUtilsContext.windowApi.document.querySelector(element);
598
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
599
599
  }
600
600
  if (element == null) {
601
601
  return;
602
602
  }
603
+ if (isNodeList(element)) {
604
+ // 设置
605
+ element.forEach(($ele) => {
606
+ DOMUtilsContext.click($ele, handler, details, useDispatchToTriggerEvent);
607
+ });
608
+ return;
609
+ }
603
610
  if (handler == null) {
604
611
  DOMUtilsContext.trigger(element, "click", details, useDispatchToTriggerEvent);
605
612
  }
@@ -629,6 +636,13 @@ define((function () { 'use strict';
629
636
  if (element == null) {
630
637
  return;
631
638
  }
639
+ if (isNodeList(element)) {
640
+ // 设置
641
+ element.forEach(($ele) => {
642
+ DOMUtilsContext.focus($ele, handler, details, useDispatchToTriggerEvent);
643
+ });
644
+ return;
645
+ }
632
646
  if (handler === null) {
633
647
  DOMUtilsContext.trigger(element, "blur", details, useDispatchToTriggerEvent);
634
648
  }
@@ -653,11 +667,18 @@ define((function () { 'use strict';
653
667
  focus(element, handler, details, useDispatchToTriggerEvent) {
654
668
  let DOMUtilsContext = this;
655
669
  if (typeof element === "string") {
656
- element = DOMUtilsContext.windowApi.document.querySelector(element);
670
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
657
671
  }
658
672
  if (element == null) {
659
673
  return;
660
674
  }
675
+ if (isNodeList(element)) {
676
+ // 设置
677
+ element.forEach(($ele) => {
678
+ DOMUtilsContext.focus($ele, handler, details, useDispatchToTriggerEvent);
679
+ });
680
+ return;
681
+ }
661
682
  if (handler == null) {
662
683
  DOMUtilsContext.trigger(element, "focus", details, useDispatchToTriggerEvent);
663
684
  }
@@ -682,18 +703,25 @@ define((function () { 'use strict';
682
703
  hover(element, handler, option) {
683
704
  let DOMUtilsContext = this;
684
705
  if (typeof element === "string") {
685
- element = DOMUtilsContext.windowApi.document.querySelector(element);
706
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
686
707
  }
687
708
  if (element == null) {
688
709
  return;
689
710
  }
711
+ if (isNodeList(element)) {
712
+ // 设置
713
+ element.forEach(($ele) => {
714
+ DOMUtilsContext.hover($ele, handler, option);
715
+ });
716
+ return;
717
+ }
690
718
  DOMUtilsContext.on(element, "mouseenter", null, handler, option);
691
719
  DOMUtilsContext.on(element, "mouseleave", null, handler, option);
692
720
  }
693
721
  /**
694
722
  * 当按键松开时触发事件
695
723
  * keydown - > keypress - > keyup
696
- * @param target 当前元素
724
+ * @param element 当前元素
697
725
  * @param handler 事件处理函数
698
726
  * @param option 配置
699
727
  * @example
@@ -705,20 +733,27 @@ define((function () { 'use strict';
705
733
  * console.log("按键松开");
706
734
  * })
707
735
  */
708
- keyup(target, handler, option) {
736
+ keyup(element, handler, option) {
709
737
  let DOMUtilsContext = this;
710
- if (target == null) {
738
+ if (element == null) {
711
739
  return;
712
740
  }
713
- if (typeof target === "string") {
714
- target = DOMUtilsContext.windowApi.document.querySelector(target);
741
+ if (typeof element === "string") {
742
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
715
743
  }
716
- DOMUtilsContext.on(target, "keyup", null, handler, option);
744
+ if (isNodeList(element)) {
745
+ // 设置
746
+ element.forEach(($ele) => {
747
+ DOMUtilsContext.keyup($ele, handler, option);
748
+ });
749
+ return;
750
+ }
751
+ DOMUtilsContext.on(element, "keyup", null, handler, option);
717
752
  }
718
753
  /**
719
754
  * 当按键按下时触发事件
720
755
  * keydown - > keypress - > keyup
721
- * @param target 目标
756
+ * @param element 目标
722
757
  * @param handler 事件处理函数
723
758
  * @param option 配置
724
759
  * @example
@@ -730,20 +765,27 @@ define((function () { 'use strict';
730
765
  * console.log("按键按下");
731
766
  * })
732
767
  */
733
- keydown(target, handler, option) {
768
+ keydown(element, handler, option) {
734
769
  let DOMUtilsContext = this;
735
- if (target == null) {
770
+ if (element == null) {
736
771
  return;
737
772
  }
738
- if (typeof target === "string") {
739
- target = DOMUtilsContext.windowApi.document.querySelector(target);
773
+ if (typeof element === "string") {
774
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
740
775
  }
741
- DOMUtilsContext.on(target, "keydown", null, handler, option);
776
+ if (isNodeList(element)) {
777
+ // 设置
778
+ element.forEach(($ele) => {
779
+ DOMUtilsContext.keydown($ele, handler, option);
780
+ });
781
+ return;
782
+ }
783
+ DOMUtilsContext.on(element, "keydown", null, handler, option);
742
784
  }
743
785
  /**
744
786
  * 当按键按下时触发事件
745
787
  * keydown - > keypress - > keyup
746
- * @param target 目标
788
+ * @param element 目标
747
789
  * @param handler 事件处理函数
748
790
  * @param option 配置
749
791
  * @example
@@ -755,20 +797,27 @@ define((function () { 'use strict';
755
797
  * console.log("按键按下");
756
798
  * })
757
799
  */
758
- keypress(target, handler, option) {
800
+ keypress(element, handler, option) {
759
801
  let DOMUtilsContext = this;
760
- if (target == null) {
802
+ if (element == null) {
761
803
  return;
762
804
  }
763
- if (typeof target === "string") {
764
- target = DOMUtilsContext.windowApi.document.querySelector(target);
805
+ if (typeof element === "string") {
806
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
807
+ }
808
+ if (isNodeList(element)) {
809
+ // 设置
810
+ element.forEach(($ele) => {
811
+ DOMUtilsContext.keypress($ele, handler, option);
812
+ });
813
+ return;
765
814
  }
766
- DOMUtilsContext.on(target, "keypress", null, handler, option);
815
+ DOMUtilsContext.on(element, "keypress", null, handler, option);
767
816
  }
768
817
  /**
769
818
  * 监听某个元素键盘按键事件或window全局按键事件
770
819
  * 按下有值的键时触发,按下Ctrl\Alt\Shift\Meta是无值键。按下先触发keydown事件,再触发keypress事件。
771
- * @param target 需要监听的对象,可以是全局Window或者某个元素
820
+ * @param element 需要监听的对象,可以是全局Window或者某个元素
772
821
  * @param eventName 事件名,默认keypress
773
822
  * @param callback 自己定义的回调事件,参数1为当前的key,参数2为组合按键,数组类型,包含ctrl、shift、alt和meta(win键或mac的cmd键)
774
823
  * @param options 监听事件的配置
@@ -828,7 +877,11 @@ define((function () { 'use strict';
828
877
  搜索 170
829
878
  收藏 171
830
879
  **/
831
- listenKeyboard(target, eventName = "keypress", callback, options) {
880
+ listenKeyboard(element, eventName = "keypress", callback, options) {
881
+ let DOMUtilsContext = this;
882
+ if (typeof element === "string") {
883
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
884
+ }
832
885
  let keyboardEventCallBack = function (event) {
833
886
  /** 键名 */
834
887
  let keyName = event.key || event.code;
@@ -852,29 +905,90 @@ define((function () { 'use strict';
852
905
  callback(keyName, keyValue, otherCodeList, event);
853
906
  }
854
907
  };
855
- this.on(target, eventName, keyboardEventCallBack, options);
908
+ DOMUtilsContext.on(element, eventName, keyboardEventCallBack, options);
856
909
  return {
857
910
  removeListen: () => {
858
- this.off(target, eventName, keyboardEventCallBack, options);
911
+ DOMUtilsContext.off(element, eventName, keyboardEventCallBack, options);
859
912
  },
860
913
  };
861
914
  }
915
+ selector(selector) {
916
+ return this.selectorAll(selector)[0];
917
+ }
918
+ selectorAll(selector) {
919
+ const context = this;
920
+ selector = selector.trim();
921
+ if (selector.match(/[^\s]{1}:empty$/gi)) {
922
+ // empty 语法
923
+ selector = selector.replace(/:empty$/gi, "");
924
+ return Array.from(context.windowApi.document.querySelectorAll(selector)).filter(($ele) => {
925
+ return $ele?.innerHTML?.trim() === "";
926
+ });
927
+ }
928
+ else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/gi) ||
929
+ selector.match(/[^\s]{1}:contains\('(.*)'\)$/gi)) {
930
+ // contains 语法
931
+ let textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
932
+ let text = textMatch[2];
933
+ selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
934
+ return Array.from(context.windowApi.document.querySelectorAll(selector)).filter(($ele) => {
935
+ // @ts-ignore
936
+ return ($ele?.textContent || $ele?.innerText)?.includes(text);
937
+ });
938
+ }
939
+ else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/gi) ||
940
+ selector.match(/[^\s]{1}:regexp\('(.*)'\)$/gi)) {
941
+ // regexp 语法
942
+ let textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
943
+ let text = textMatch[2];
944
+ let regexp = new RegExp(text);
945
+ selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
946
+ return Array.from(context.windowApi.document.querySelectorAll(selector)).filter(($ele) => {
947
+ // @ts-ignore
948
+ return Boolean(($ele?.textContent || $ele?.innerText)?.match(regexp));
949
+ });
950
+ }
951
+ else {
952
+ // 普通语法
953
+ return Array.from(context.windowApi.document.querySelectorAll(selector));
954
+ }
955
+ }
862
956
  }
863
957
 
958
+ /**
959
+ * 判断是否是元素列表
960
+ * @param $ele
961
+ */
962
+ const isNodeList = ($ele) => {
963
+ return Array.isArray($ele) || $ele instanceof NodeList;
964
+ };
864
965
  class DOMUtils extends DOMUtilsEvent {
865
966
  constructor(option) {
866
967
  super(option);
867
968
  }
868
969
  /** 版本号 */
869
- version = "2024.10.19";
970
+ version = "2024.10.22";
870
971
  attr(element, attrName, attrValue) {
871
972
  let DOMUtilsContext = this;
872
973
  if (typeof element === "string") {
873
- element = DOMUtilsContext.windowApi.document.querySelector(element);
974
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
874
975
  }
875
976
  if (element == null) {
876
977
  return;
877
978
  }
979
+ if (isNodeList(element)) {
980
+ if (attrValue == null) {
981
+ // 获取属性
982
+ return DOMUtilsContext.attr(element[0], attrName, attrValue);
983
+ }
984
+ else {
985
+ // 设置属性
986
+ element.forEach(($ele) => {
987
+ DOMUtilsContext.attr($ele, attrName, attrValue);
988
+ });
989
+ return;
990
+ }
991
+ }
878
992
  if (attrValue == null) {
879
993
  return element.getAttribute(attrName);
880
994
  }
@@ -963,14 +1077,40 @@ define((function () { 'use strict';
963
1077
  return propertyValue;
964
1078
  }
965
1079
  if (typeof element === "string") {
966
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1080
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
967
1081
  }
968
1082
  if (element == null) {
969
1083
  return;
970
1084
  }
1085
+ if (isNodeList(element)) {
1086
+ if (typeof property === "string") {
1087
+ if (value == null) {
1088
+ // 获取属性
1089
+ return DOMUtilsContext.css(element[0], property);
1090
+ }
1091
+ else {
1092
+ // 设置属性
1093
+ element.forEach(($ele) => {
1094
+ DOMUtilsContext.css($ele, property);
1095
+ });
1096
+ return;
1097
+ }
1098
+ }
1099
+ else if (typeof property === "object") {
1100
+ // 设置属性
1101
+ element.forEach(($ele) => {
1102
+ DOMUtilsContext.css($ele, property);
1103
+ });
1104
+ return;
1105
+ }
1106
+ else ;
1107
+ return;
1108
+ }
971
1109
  if (typeof property === "string") {
972
1110
  if (value == null) {
973
- return getComputedStyle(element).getPropertyValue(property);
1111
+ return DOMUtilsContext.windowApi.globalThis
1112
+ .getComputedStyle(element)
1113
+ .getPropertyValue(property);
974
1114
  }
975
1115
  else {
976
1116
  if (value === "string" && value.includes("!important")) {
@@ -994,15 +1134,29 @@ define((function () { 'use strict';
994
1134
  }
995
1135
  }
996
1136
  }
1137
+ else ;
997
1138
  }
998
1139
  text(element, text) {
999
1140
  let DOMUtilsContext = this;
1000
1141
  if (typeof element === "string") {
1001
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1142
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1002
1143
  }
1003
1144
  if (element == null) {
1004
1145
  return;
1005
1146
  }
1147
+ if (isNodeList(element)) {
1148
+ if (text == null) {
1149
+ // 获取
1150
+ return DOMUtilsContext.text(element[0]);
1151
+ }
1152
+ else {
1153
+ // 设置
1154
+ element.forEach(($ele) => {
1155
+ DOMUtilsContext.text($ele, text);
1156
+ });
1157
+ }
1158
+ return;
1159
+ }
1006
1160
  if (text == null) {
1007
1161
  return element.textContent || element.innerText;
1008
1162
  }
@@ -1021,15 +1175,30 @@ define((function () { 'use strict';
1021
1175
  html(element, html) {
1022
1176
  let DOMUtilsContext = this;
1023
1177
  if (typeof element === "string") {
1024
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1178
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1025
1179
  }
1026
1180
  if (element == null) {
1027
1181
  return;
1028
1182
  }
1183
+ if (isNodeList(element)) {
1184
+ if (html == null) {
1185
+ // 获取
1186
+ return DOMUtilsContext.html(element[0]);
1187
+ }
1188
+ else {
1189
+ // 设置
1190
+ element.forEach(($ele) => {
1191
+ DOMUtilsContext.html($ele, html);
1192
+ });
1193
+ }
1194
+ return;
1195
+ }
1029
1196
  if (html == null) {
1197
+ // 获取
1030
1198
  return element.innerHTML;
1031
1199
  }
1032
1200
  else {
1201
+ // 设置
1033
1202
  if (html instanceof Element) {
1034
1203
  html = html.innerHTML;
1035
1204
  }
@@ -1052,7 +1221,7 @@ define((function () { 'use strict';
1052
1221
  recovery();
1053
1222
  return transformInfo;
1054
1223
  }
1055
- let elementTransform = getComputedStyle(element).transform;
1224
+ let elementTransform = DOMUtilsContext.windowApi.globalThis.getComputedStyle(element).transform;
1056
1225
  if (elementTransform != null &&
1057
1226
  elementTransform !== "none" &&
1058
1227
  elementTransform !== "") {
@@ -1076,12 +1245,26 @@ define((function () { 'use strict';
1076
1245
  val(element, value) {
1077
1246
  let DOMUtilsContext = this;
1078
1247
  if (typeof element === "string") {
1079
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1248
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1080
1249
  }
1081
1250
  if (element == null) {
1082
1251
  return;
1083
1252
  }
1253
+ if (isNodeList(element)) {
1254
+ if (value == null) {
1255
+ // 获取
1256
+ return DOMUtilsContext.val(element[0]);
1257
+ }
1258
+ else {
1259
+ // 设置
1260
+ element.forEach(($ele) => {
1261
+ DOMUtilsContext.val($ele, value);
1262
+ });
1263
+ }
1264
+ return;
1265
+ }
1084
1266
  if (value == null) {
1267
+ // 获取
1085
1268
  if (element.localName === "input" &&
1086
1269
  (element.type === "checkbox" || element.type === "radio")) {
1087
1270
  return element.checked;
@@ -1091,6 +1274,7 @@ define((function () { 'use strict';
1091
1274
  }
1092
1275
  }
1093
1276
  else {
1277
+ // 设置
1094
1278
  if (element.localName === "input" &&
1095
1279
  (element.type === "checkbox" || element.type === "radio")) {
1096
1280
  element.checked = !!value;
@@ -1102,17 +1286,30 @@ define((function () { 'use strict';
1102
1286
  }
1103
1287
  prop(element, propName, propValue) {
1104
1288
  let DOMUtilsContext = this;
1289
+ if (typeof element === "string") {
1290
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1291
+ }
1105
1292
  if (element == null) {
1106
1293
  return;
1107
1294
  }
1108
- if (typeof element === "string") {
1109
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1295
+ if (isNodeList(element)) {
1296
+ if (propValue == null) {
1297
+ // 获取
1298
+ return DOMUtilsContext.prop(element[0], propName);
1299
+ }
1300
+ else {
1301
+ // 设置
1302
+ element.forEach(($ele) => {
1303
+ DOMUtilsContext.prop($ele, propName, propValue);
1304
+ });
1305
+ }
1306
+ return;
1110
1307
  }
1111
1308
  if (propValue == null) {
1112
- return element[propName];
1309
+ return Reflect.get(element, propName);
1113
1310
  }
1114
1311
  else {
1115
- element[propName] = propValue;
1312
+ Reflect.set(element, propName, propValue);
1116
1313
  }
1117
1314
  }
1118
1315
  /**
@@ -1127,11 +1324,18 @@ define((function () { 'use strict';
1127
1324
  removeAttr(element, attrName) {
1128
1325
  let DOMUtilsContext = this;
1129
1326
  if (typeof element === "string") {
1130
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1327
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1131
1328
  }
1132
1329
  if (element == null) {
1133
1330
  return;
1134
1331
  }
1332
+ if (isNodeList(element)) {
1333
+ // 设置
1334
+ element.forEach(($ele) => {
1335
+ DOMUtilsContext.removeAttr($ele, attrName);
1336
+ });
1337
+ return;
1338
+ }
1135
1339
  element.removeAttribute(attrName);
1136
1340
  }
1137
1341
  /**
@@ -1146,15 +1350,30 @@ define((function () { 'use strict';
1146
1350
  removeClass(element, className) {
1147
1351
  let DOMUtilsContext = this;
1148
1352
  if (typeof element === "string") {
1149
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1353
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1150
1354
  }
1151
1355
  if (element == null) {
1152
1356
  return;
1153
1357
  }
1154
- if (className == null) {
1358
+ if (isNodeList(element)) {
1359
+ // 设置
1360
+ element.forEach(($ele) => {
1361
+ DOMUtilsContext.removeClass($ele, className);
1362
+ });
1155
1363
  return;
1156
1364
  }
1157
- element.classList.remove(className);
1365
+ if (className == null) {
1366
+ // 清空全部className
1367
+ element.className = "";
1368
+ }
1369
+ else {
1370
+ if (!Array.isArray(className)) {
1371
+ className = className.split(" ");
1372
+ }
1373
+ className.forEach((itemClassName) => {
1374
+ element.classList.remove(itemClassName);
1375
+ });
1376
+ }
1158
1377
  }
1159
1378
  /**
1160
1379
  * 移除元素的属性
@@ -1168,11 +1387,18 @@ define((function () { 'use strict';
1168
1387
  removeProp(element, propName) {
1169
1388
  let DOMUtilsContext = this;
1170
1389
  if (typeof element === "string") {
1171
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1390
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1172
1391
  }
1173
1392
  if (element == null) {
1174
1393
  return;
1175
1394
  }
1395
+ if (isNodeList(element)) {
1396
+ // 设置
1397
+ element.forEach(($ele) => {
1398
+ DOMUtilsContext.removeProp($ele, propName);
1399
+ });
1400
+ return;
1401
+ }
1176
1402
  DOMUtilsCommonUtils.delete(element, propName);
1177
1403
  }
1178
1404
  /**
@@ -1187,22 +1413,22 @@ define((function () { 'use strict';
1187
1413
  replaceWith(element, newElement) {
1188
1414
  let DOMUtilsContext = this;
1189
1415
  if (typeof element === "string") {
1190
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1416
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1191
1417
  }
1192
1418
  if (element == null) {
1193
1419
  return;
1194
1420
  }
1195
- if (typeof newElement === "string") {
1196
- newElement = DOMUtilsContext.parseHTML(newElement, false, false);
1197
- }
1198
- if (element instanceof NodeList || element instanceof Array) {
1199
- element.forEach((item) => {
1200
- DOMUtilsContext.replaceWith(item, newElement);
1421
+ if (isNodeList(element)) {
1422
+ // 设置
1423
+ element.forEach(($ele) => {
1424
+ DOMUtilsContext.replaceWith($ele, newElement);
1201
1425
  });
1426
+ return;
1202
1427
  }
1203
- else {
1204
- element.parentElement.replaceChild(newElement, element);
1428
+ if (typeof newElement === "string") {
1429
+ newElement = DOMUtilsContext.parseHTML(newElement, false, false);
1205
1430
  }
1431
+ element.parentElement.replaceChild(newElement, element);
1206
1432
  }
1207
1433
  /**
1208
1434
  * 给元素添加class
@@ -1216,12 +1442,27 @@ define((function () { 'use strict';
1216
1442
  addClass(element, className) {
1217
1443
  let DOMUtilsContext = this;
1218
1444
  if (typeof element === "string") {
1219
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1445
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1220
1446
  }
1221
1447
  if (element == null) {
1222
1448
  return;
1223
1449
  }
1224
- element.classList.add(className);
1450
+ if (isNodeList(element)) {
1451
+ // 设置
1452
+ element.forEach(($ele) => {
1453
+ DOMUtilsContext.addClass($ele, className);
1454
+ });
1455
+ return;
1456
+ }
1457
+ if (!Array.isArray(className)) {
1458
+ className = className.split(" ");
1459
+ }
1460
+ className.forEach((itemClassName) => {
1461
+ if (itemClassName.trim() == "") {
1462
+ return;
1463
+ }
1464
+ element.classList.add(itemClassName);
1465
+ });
1225
1466
  }
1226
1467
  /**
1227
1468
  * 函数在元素内部末尾添加子元素或HTML字符串
@@ -1235,11 +1476,18 @@ define((function () { 'use strict';
1235
1476
  append(element, content) {
1236
1477
  let DOMUtilsContext = this;
1237
1478
  if (typeof element === "string") {
1238
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1479
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1239
1480
  }
1240
1481
  if (element == null) {
1241
1482
  return;
1242
1483
  }
1484
+ if (isNodeList(element)) {
1485
+ // 设置
1486
+ element.forEach(($ele) => {
1487
+ DOMUtilsContext.append($ele, content);
1488
+ });
1489
+ return;
1490
+ }
1243
1491
  function elementAppendChild(ele, text) {
1244
1492
  if (typeof content === "string") {
1245
1493
  ele.insertAdjacentHTML("beforeend", text);
@@ -1253,7 +1501,7 @@ define((function () { 'use strict';
1253
1501
  let fragment = DOMUtilsContext.windowApi.document.createDocumentFragment();
1254
1502
  content.forEach((ele) => {
1255
1503
  if (typeof ele === "string") {
1256
- ele = this.parseHTML(ele, true, false);
1504
+ ele = DOMUtilsContext.parseHTML(ele, true, false);
1257
1505
  }
1258
1506
  fragment.appendChild(ele);
1259
1507
  });
@@ -1275,11 +1523,18 @@ define((function () { 'use strict';
1275
1523
  prepend(element, content) {
1276
1524
  let DOMUtilsContext = this;
1277
1525
  if (typeof element === "string") {
1278
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1526
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1279
1527
  }
1280
1528
  if (element == null) {
1281
1529
  return;
1282
1530
  }
1531
+ if (isNodeList(element)) {
1532
+ // 设置
1533
+ element.forEach(($ele) => {
1534
+ DOMUtilsContext.prepend($ele, content);
1535
+ });
1536
+ return;
1537
+ }
1283
1538
  if (typeof content === "string") {
1284
1539
  element.insertAdjacentHTML("afterbegin", content);
1285
1540
  }
@@ -1305,11 +1560,18 @@ define((function () { 'use strict';
1305
1560
  after(element, content) {
1306
1561
  let DOMUtilsContext = this;
1307
1562
  if (typeof element === "string") {
1308
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1563
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1309
1564
  }
1310
1565
  if (element == null) {
1311
1566
  return;
1312
1567
  }
1568
+ if (isNodeList(element)) {
1569
+ // 设置
1570
+ element.forEach(($ele) => {
1571
+ DOMUtilsContext.after($ele, content);
1572
+ });
1573
+ return;
1574
+ }
1313
1575
  if (typeof content === "string") {
1314
1576
  element.insertAdjacentHTML("afterend", content);
1315
1577
  }
@@ -1337,11 +1599,18 @@ define((function () { 'use strict';
1337
1599
  before(element, content) {
1338
1600
  let DOMUtilsContext = this;
1339
1601
  if (typeof element === "string") {
1340
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1602
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1341
1603
  }
1342
1604
  if (element == null) {
1343
1605
  return;
1344
1606
  }
1607
+ if (isNodeList(element)) {
1608
+ // 设置
1609
+ element.forEach(($ele) => {
1610
+ DOMUtilsContext.before($ele, content);
1611
+ });
1612
+ return;
1613
+ }
1345
1614
  if (typeof content === "string") {
1346
1615
  element.insertAdjacentHTML("beforebegin", content);
1347
1616
  }
@@ -1357,30 +1626,28 @@ define((function () { 'use strict';
1357
1626
  }
1358
1627
  /**
1359
1628
  * 移除元素
1360
- * @param target 目标元素
1629
+ * @param element 目标元素
1361
1630
  * @example
1362
1631
  * // 元素a.xx前面添加一个元素
1363
1632
  * DOMUtils.remove(document.querySelector("a.xx"))
1364
1633
  * DOMUtils.remove(document.querySelectorAll("a.xx"))
1365
1634
  * DOMUtils.remove("a.xx")
1366
1635
  * */
1367
- remove(target) {
1636
+ remove(element) {
1368
1637
  let DOMUtilsContext = this;
1369
- if (typeof target === "string") {
1370
- target = DOMUtilsContext.windowApi.document.querySelectorAll(target);
1638
+ if (typeof element === "string") {
1639
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1371
1640
  }
1372
- if (target == null) {
1641
+ if (element == null) {
1373
1642
  return;
1374
1643
  }
1375
- if (target instanceof NodeList || target instanceof Array) {
1376
- target = target;
1377
- for (const element of target) {
1378
- DOMUtilsContext.remove(element);
1379
- }
1380
- }
1381
- else {
1382
- target.remove();
1644
+ if (isNodeList(element)) {
1645
+ element.forEach(($ele) => {
1646
+ DOMUtilsContext.remove($ele);
1647
+ });
1648
+ return;
1383
1649
  }
1650
+ element.remove();
1384
1651
  }
1385
1652
  /**
1386
1653
  * 移除元素的所有子元素
@@ -1393,11 +1660,18 @@ define((function () { 'use strict';
1393
1660
  empty(element) {
1394
1661
  let DOMUtilsContext = this;
1395
1662
  if (typeof element === "string") {
1396
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1663
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1397
1664
  }
1398
1665
  if (element == null) {
1399
1666
  return;
1400
1667
  }
1668
+ if (isNodeList(element)) {
1669
+ // 设置
1670
+ element.forEach(($ele) => {
1671
+ DOMUtilsContext.empty($ele);
1672
+ });
1673
+ return;
1674
+ }
1401
1675
  element.innerHTML = "";
1402
1676
  }
1403
1677
  /**
@@ -1428,7 +1702,8 @@ define((function () { 'use strict';
1428
1702
  width(element, isShow = false) {
1429
1703
  let DOMUtilsContext = this;
1430
1704
  if (typeof element === "string") {
1431
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1705
+ element =
1706
+ DOMUtilsContext.windowApi.document.querySelector(element);
1432
1707
  }
1433
1708
  if (element == null) {
1434
1709
  return;
@@ -1540,7 +1815,7 @@ define((function () { 'use strict';
1540
1815
  }
1541
1816
  element = element;
1542
1817
  if (isShow || (!isShow && DOMUtilsCommonUtils.isShow(element))) {
1543
- let style = getComputedStyle(element, null);
1818
+ let style = DOMUtilsContext.windowApi.globalThis.getComputedStyle(element, null);
1544
1819
  let marginLeft = DOMUtilsCommonUtils.getStyleValue(style, "marginLeft");
1545
1820
  let marginRight = DOMUtilsCommonUtils.getStyleValue(style, "marginRight");
1546
1821
  return element.offsetWidth + marginLeft + marginRight;
@@ -1566,7 +1841,7 @@ define((function () { 'use strict';
1566
1841
  }
1567
1842
  element = element;
1568
1843
  if (isShow || (!isShow && DOMUtilsCommonUtils.isShow(element))) {
1569
- let style = getComputedStyle(element, null);
1844
+ let style = DOMUtilsContext.windowApi.globalThis.getComputedStyle(element, null);
1570
1845
  let marginTop = DOMUtilsCommonUtils.getStyleValue(style, "marginTop");
1571
1846
  let marginBottom = DOMUtilsCommonUtils.getStyleValue(style, "marginBottom");
1572
1847
  return element.offsetHeight + marginTop + marginBottom;
@@ -1593,11 +1868,18 @@ define((function () { 'use strict';
1593
1868
  animate(element, styles, duration = 1000, callback = null) {
1594
1869
  let DOMUtilsContext = this;
1595
1870
  if (typeof element === "string") {
1596
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1871
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1597
1872
  }
1598
1873
  if (element == null) {
1599
1874
  return;
1600
1875
  }
1876
+ if (isNodeList(element)) {
1877
+ // 设置
1878
+ element.forEach(($ele) => {
1879
+ DOMUtilsContext.animate($ele, styles, duration, callback);
1880
+ });
1881
+ return;
1882
+ }
1601
1883
  if (typeof duration !== "number" || duration <= 0) {
1602
1884
  throw new TypeError("duration must be a positive number");
1603
1885
  }
@@ -1614,7 +1896,9 @@ define((function () { 'use strict';
1614
1896
  let from = {};
1615
1897
  let to = {};
1616
1898
  for (let prop in styles) {
1617
- from[prop] = element.style[prop] || getComputedStyle(element)[prop];
1899
+ from[prop] =
1900
+ element.style[prop] ||
1901
+ DOMUtilsContext.windowApi.globalThis.getComputedStyle(element)[prop];
1618
1902
  to[prop] = styles[prop];
1619
1903
  }
1620
1904
  let timer = setInterval(function () {
@@ -1646,18 +1930,26 @@ define((function () { 'use strict';
1646
1930
  wrap(element, wrapperHTML) {
1647
1931
  let DOMUtilsContext = this;
1648
1932
  if (typeof element === "string") {
1649
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1933
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1650
1934
  }
1651
1935
  if (element == null) {
1652
1936
  return;
1653
1937
  }
1938
+ if (isNodeList(element)) {
1939
+ // 设置
1940
+ element.forEach(($ele) => {
1941
+ DOMUtilsContext.wrap($ele, wrapperHTML);
1942
+ });
1943
+ return;
1944
+ }
1654
1945
  element = element;
1655
1946
  // 创建一个新的div元素,并将wrapperHTML作为其innerHTML
1656
1947
  let wrapper = DOMUtilsContext.windowApi.document.createElement("div");
1657
1948
  wrapper.innerHTML = wrapperHTML;
1658
1949
  let wrapperFirstChild = wrapper.firstChild;
1659
1950
  // 将要包裹的元素插入目标元素前面
1660
- element.parentElement.insertBefore(wrapperFirstChild, element);
1951
+ let parentElement = element.parentElement;
1952
+ parentElement.insertBefore(wrapperFirstChild, element);
1661
1953
  // 将要包裹的元素移动到wrapper中
1662
1954
  wrapperFirstChild.appendChild(element);
1663
1955
  }
@@ -1737,6 +2029,8 @@ define((function () { 'use strict';
1737
2029
  }
1738
2030
  parseHTML(html, useParser = false, isComplete = false) {
1739
2031
  let DOMUtilsContext = this;
2032
+ // 去除html前后的空格
2033
+ html = html.trim();
1740
2034
  function parseHTMLByDOMParser() {
1741
2035
  let parser = new DOMParser();
1742
2036
  if (isComplete) {
@@ -1763,16 +2057,61 @@ define((function () { 'use strict';
1763
2057
  return parseHTMLByCreateDom();
1764
2058
  }
1765
2059
  }
2060
+ /**
2061
+ * 序列化表单元素
2062
+ * @param $form 表单元素
2063
+ * @example
2064
+ * DOMUtils.serialize(document.querySelector("form"))
2065
+ * > xxx=xxx&aaa=
2066
+ */
2067
+ serialize($form) {
2068
+ const elements = $form.elements;
2069
+ let serializedArray = [];
2070
+ for (let i = 0; i < elements.length; i++) {
2071
+ const element = elements[i];
2072
+ if (element.name &&
2073
+ !element.disabled &&
2074
+ (element.checked ||
2075
+ [
2076
+ "text",
2077
+ "hidden",
2078
+ "password",
2079
+ "textarea",
2080
+ "select-one",
2081
+ "select-multiple",
2082
+ ].includes(element.type))) {
2083
+ if (element.type === "select-multiple") {
2084
+ for (let j = 0; j < element.options.length; j++) {
2085
+ if (element.options[j].selected) {
2086
+ serializedArray.push({
2087
+ name: element.name,
2088
+ value: element.options[j].value,
2089
+ });
2090
+ }
2091
+ }
2092
+ }
2093
+ else {
2094
+ serializedArray.push({ name: element.name, value: element.value });
2095
+ }
2096
+ }
2097
+ }
2098
+ return serializedArray
2099
+ .map((item) => `${encodeURIComponent(item.name)}=${encodeURIComponent(item.value)}`)
2100
+ .join("&");
2101
+ }
1766
2102
  /**
1767
2103
  * 显示元素
1768
2104
  * @param target 当前元素
2105
+ * @param checkVisiblie 是否检测元素是否显示
2106
+ * + true (默认)如果检测到还未显示,则强制使用display: unset !important;
2107
+ * + false 不检测,直接设置display属性为空
1769
2108
  * @example
1770
2109
  * // 显示a.xx元素
1771
2110
  * DOMUtils.show(document.querySelector("a.xx"))
1772
2111
  * DOMUtils.show(document.querySelectorAll("a.xx"))
1773
2112
  * DOMUtils.show("a.xx")
1774
2113
  */
1775
- show(target) {
2114
+ show(target, checkVisiblie = true) {
1776
2115
  let DOMUtilsContext = this;
1777
2116
  if (target == null) {
1778
2117
  return;
@@ -1783,28 +2122,33 @@ define((function () { 'use strict';
1783
2122
  if (target instanceof NodeList || target instanceof Array) {
1784
2123
  target = target;
1785
2124
  for (const element of target) {
1786
- DOMUtilsContext.show(element);
2125
+ DOMUtilsContext.show(element, checkVisiblie);
1787
2126
  }
1788
2127
  }
1789
2128
  else {
1790
2129
  target = target;
1791
2130
  target.style.display = "";
1792
- if (!DOMUtilsCommonUtils.isShow(target)) {
1793
- /* 仍然是不显示,尝试使用强覆盖 */
1794
- target.style.setProperty("display", "unset", "important");
2131
+ if (checkVisiblie) {
2132
+ if (!DOMUtilsCommonUtils.isShow(target)) {
2133
+ /* 仍然是不显示,尝试使用强覆盖 */
2134
+ target.style.setProperty("display", "unset", "important");
2135
+ }
1795
2136
  }
1796
2137
  }
1797
2138
  }
1798
2139
  /**
1799
2140
  * 隐藏元素
1800
2141
  * @param target 当前元素
2142
+ * @param checkVisiblie 是否检测元素是否显示
2143
+ * + true (默认)如果检测到显示,则强制使用display: none !important;
2144
+ * + false 不检测,直接设置display属性为none
1801
2145
  * @example
1802
2146
  * // 隐藏a.xx元素
1803
2147
  * DOMUtils.hide(document.querySelector("a.xx"))
1804
2148
  * DOMUtils.hide(document.querySelectorAll("a.xx"))
1805
2149
  * DOMUtils.hide("a.xx")
1806
2150
  */
1807
- hide(target) {
2151
+ hide(target, checkVisiblie = true) {
1808
2152
  let DOMUtilsContext = this;
1809
2153
  if (target == null) {
1810
2154
  return;
@@ -1815,15 +2159,17 @@ define((function () { 'use strict';
1815
2159
  if (target instanceof NodeList || target instanceof Array) {
1816
2160
  target = target;
1817
2161
  for (const element of target) {
1818
- DOMUtilsContext.hide(element);
2162
+ DOMUtilsContext.hide(element, checkVisiblie);
1819
2163
  }
1820
2164
  }
1821
2165
  else {
1822
2166
  target = target;
1823
2167
  target.style.display = "none";
1824
- if (DOMUtilsCommonUtils.isShow(target)) {
1825
- /* 仍然是显示,尝试使用强覆盖 */
1826
- target.style.setProperty("display", "none", "important");
2168
+ if (checkVisiblie) {
2169
+ if (DOMUtilsCommonUtils.isShow(target)) {
2170
+ /* 仍然是显示,尝试使用强覆盖 */
2171
+ target.style.setProperty("display", "none", "important");
2172
+ }
1827
2173
  }
1828
2174
  }
1829
2175
  }
@@ -1847,9 +2193,15 @@ define((function () { 'use strict';
1847
2193
  }
1848
2194
  let DOMUtilsContext = this;
1849
2195
  if (typeof element === "string") {
1850
- element = DOMUtilsContext.windowApi.document.querySelector(element);
2196
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
2197
+ }
2198
+ if (isNodeList(element)) {
2199
+ // 设置
2200
+ element.forEach(($ele) => {
2201
+ DOMUtilsContext.fadeIn($ele, duration, callback);
2202
+ });
2203
+ return;
1851
2204
  }
1852
- element = element;
1853
2205
  element.style.opacity = "0";
1854
2206
  element.style.display = "";
1855
2207
  let start = null;
@@ -1892,9 +2244,15 @@ define((function () { 'use strict';
1892
2244
  return;
1893
2245
  }
1894
2246
  if (typeof element === "string") {
1895
- element = DOMUtilsContext.windowApi.document.querySelector(element);
2247
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
2248
+ }
2249
+ if (isNodeList(element)) {
2250
+ // 设置
2251
+ element.forEach(($ele) => {
2252
+ DOMUtilsContext.fadeOut($ele, duration, callback);
2253
+ });
2254
+ return;
1896
2255
  }
1897
- element = element;
1898
2256
  element.style.opacity = "1";
1899
2257
  let start = null;
1900
2258
  let timer = null;
@@ -1920,24 +2278,34 @@ define((function () { 'use strict';
1920
2278
  /**
1921
2279
  * 切换元素的显示和隐藏状态
1922
2280
  * @param element 当前元素
2281
+ * @param checkVisiblie 是否检测元素是否显示
1923
2282
  * @example
1924
2283
  * // 如果元素a.xx当前是隐藏,则显示,如果是显示,则隐藏
1925
2284
  * DOMUtils.toggle(document.querySelector("a.xx"))
1926
2285
  * DOMUtils.toggle("a.xx")
1927
2286
  */
1928
- toggle(element) {
2287
+ toggle(element, checkVisiblie) {
1929
2288
  let DOMUtilsContext = this;
1930
2289
  if (typeof element === "string") {
1931
- element = DOMUtilsContext.windowApi.document.querySelector(element);
2290
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1932
2291
  }
1933
2292
  if (element == null) {
1934
2293
  return;
1935
2294
  }
1936
- if (getComputedStyle(element).getPropertyValue("display") === "none") {
1937
- DOMUtilsContext.show(element);
2295
+ if (isNodeList(element)) {
2296
+ // 设置
2297
+ element.forEach(($ele) => {
2298
+ DOMUtilsContext.toggle($ele);
2299
+ });
2300
+ return;
2301
+ }
2302
+ if (DOMUtilsContext.windowApi.globalThis
2303
+ .getComputedStyle(element)
2304
+ .getPropertyValue("display") === "none") {
2305
+ DOMUtilsContext.show(element, checkVisiblie);
1938
2306
  }
1939
2307
  else {
1940
- DOMUtilsContext.hide(element);
2308
+ DOMUtilsContext.hide(element, checkVisiblie);
1941
2309
  }
1942
2310
  }
1943
2311
  /**