@whitesev/domutils 1.3.4 → 1.3.5

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.umd.js CHANGED
@@ -599,11 +599,18 @@
599
599
  click(element, handler, details, useDispatchToTriggerEvent) {
600
600
  let DOMUtilsContext = this;
601
601
  if (typeof element === "string") {
602
- element = DOMUtilsContext.windowApi.document.querySelector(element);
602
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
603
603
  }
604
604
  if (element == null) {
605
605
  return;
606
606
  }
607
+ if (isNodeList(element)) {
608
+ // 设置
609
+ element.forEach(($ele) => {
610
+ DOMUtilsContext.click($ele, handler, details, useDispatchToTriggerEvent);
611
+ });
612
+ return;
613
+ }
607
614
  if (handler == null) {
608
615
  DOMUtilsContext.trigger(element, "click", details, useDispatchToTriggerEvent);
609
616
  }
@@ -633,6 +640,13 @@
633
640
  if (element == null) {
634
641
  return;
635
642
  }
643
+ if (isNodeList(element)) {
644
+ // 设置
645
+ element.forEach(($ele) => {
646
+ DOMUtilsContext.focus($ele, handler, details, useDispatchToTriggerEvent);
647
+ });
648
+ return;
649
+ }
636
650
  if (handler === null) {
637
651
  DOMUtilsContext.trigger(element, "blur", details, useDispatchToTriggerEvent);
638
652
  }
@@ -657,11 +671,18 @@
657
671
  focus(element, handler, details, useDispatchToTriggerEvent) {
658
672
  let DOMUtilsContext = this;
659
673
  if (typeof element === "string") {
660
- element = DOMUtilsContext.windowApi.document.querySelector(element);
674
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
661
675
  }
662
676
  if (element == null) {
663
677
  return;
664
678
  }
679
+ if (isNodeList(element)) {
680
+ // 设置
681
+ element.forEach(($ele) => {
682
+ DOMUtilsContext.focus($ele, handler, details, useDispatchToTriggerEvent);
683
+ });
684
+ return;
685
+ }
665
686
  if (handler == null) {
666
687
  DOMUtilsContext.trigger(element, "focus", details, useDispatchToTriggerEvent);
667
688
  }
@@ -686,18 +707,25 @@
686
707
  hover(element, handler, option) {
687
708
  let DOMUtilsContext = this;
688
709
  if (typeof element === "string") {
689
- element = DOMUtilsContext.windowApi.document.querySelector(element);
710
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
690
711
  }
691
712
  if (element == null) {
692
713
  return;
693
714
  }
715
+ if (isNodeList(element)) {
716
+ // 设置
717
+ element.forEach(($ele) => {
718
+ DOMUtilsContext.hover($ele, handler, option);
719
+ });
720
+ return;
721
+ }
694
722
  DOMUtilsContext.on(element, "mouseenter", null, handler, option);
695
723
  DOMUtilsContext.on(element, "mouseleave", null, handler, option);
696
724
  }
697
725
  /**
698
726
  * 当按键松开时触发事件
699
727
  * keydown - > keypress - > keyup
700
- * @param target 当前元素
728
+ * @param element 当前元素
701
729
  * @param handler 事件处理函数
702
730
  * @param option 配置
703
731
  * @example
@@ -709,20 +737,27 @@
709
737
  * console.log("按键松开");
710
738
  * })
711
739
  */
712
- keyup(target, handler, option) {
740
+ keyup(element, handler, option) {
713
741
  let DOMUtilsContext = this;
714
- if (target == null) {
742
+ if (element == null) {
715
743
  return;
716
744
  }
717
- if (typeof target === "string") {
718
- target = DOMUtilsContext.windowApi.document.querySelector(target);
745
+ if (typeof element === "string") {
746
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
747
+ }
748
+ if (isNodeList(element)) {
749
+ // 设置
750
+ element.forEach(($ele) => {
751
+ DOMUtilsContext.keyup($ele, handler, option);
752
+ });
753
+ return;
719
754
  }
720
- DOMUtilsContext.on(target, "keyup", null, handler, option);
755
+ DOMUtilsContext.on(element, "keyup", null, handler, option);
721
756
  }
722
757
  /**
723
758
  * 当按键按下时触发事件
724
759
  * keydown - > keypress - > keyup
725
- * @param target 目标
760
+ * @param element 目标
726
761
  * @param handler 事件处理函数
727
762
  * @param option 配置
728
763
  * @example
@@ -734,20 +769,27 @@
734
769
  * console.log("按键按下");
735
770
  * })
736
771
  */
737
- keydown(target, handler, option) {
772
+ keydown(element, handler, option) {
738
773
  let DOMUtilsContext = this;
739
- if (target == null) {
774
+ if (element == null) {
740
775
  return;
741
776
  }
742
- if (typeof target === "string") {
743
- target = DOMUtilsContext.windowApi.document.querySelector(target);
777
+ if (typeof element === "string") {
778
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
779
+ }
780
+ if (isNodeList(element)) {
781
+ // 设置
782
+ element.forEach(($ele) => {
783
+ DOMUtilsContext.keydown($ele, handler, option);
784
+ });
785
+ return;
744
786
  }
745
- DOMUtilsContext.on(target, "keydown", null, handler, option);
787
+ DOMUtilsContext.on(element, "keydown", null, handler, option);
746
788
  }
747
789
  /**
748
790
  * 当按键按下时触发事件
749
791
  * keydown - > keypress - > keyup
750
- * @param target 目标
792
+ * @param element 目标
751
793
  * @param handler 事件处理函数
752
794
  * @param option 配置
753
795
  * @example
@@ -759,20 +801,27 @@
759
801
  * console.log("按键按下");
760
802
  * })
761
803
  */
762
- keypress(target, handler, option) {
804
+ keypress(element, handler, option) {
763
805
  let DOMUtilsContext = this;
764
- if (target == null) {
806
+ if (element == null) {
765
807
  return;
766
808
  }
767
- if (typeof target === "string") {
768
- target = DOMUtilsContext.windowApi.document.querySelector(target);
809
+ if (typeof element === "string") {
810
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
811
+ }
812
+ if (isNodeList(element)) {
813
+ // 设置
814
+ element.forEach(($ele) => {
815
+ DOMUtilsContext.keypress($ele, handler, option);
816
+ });
817
+ return;
769
818
  }
770
- DOMUtilsContext.on(target, "keypress", null, handler, option);
819
+ DOMUtilsContext.on(element, "keypress", null, handler, option);
771
820
  }
772
821
  /**
773
822
  * 监听某个元素键盘按键事件或window全局按键事件
774
823
  * 按下有值的键时触发,按下Ctrl\Alt\Shift\Meta是无值键。按下先触发keydown事件,再触发keypress事件。
775
- * @param target 需要监听的对象,可以是全局Window或者某个元素
824
+ * @param element 需要监听的对象,可以是全局Window或者某个元素
776
825
  * @param eventName 事件名,默认keypress
777
826
  * @param callback 自己定义的回调事件,参数1为当前的key,参数2为组合按键,数组类型,包含ctrl、shift、alt和meta(win键或mac的cmd键)
778
827
  * @param options 监听事件的配置
@@ -832,7 +881,11 @@
832
881
  搜索 170
833
882
  收藏 171
834
883
  **/
835
- listenKeyboard(target, eventName = "keypress", callback, options) {
884
+ listenKeyboard(element, eventName = "keypress", callback, options) {
885
+ let DOMUtilsContext = this;
886
+ if (typeof element === "string") {
887
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
888
+ }
836
889
  let keyboardEventCallBack = function (event) {
837
890
  /** 键名 */
838
891
  let keyName = event.key || event.code;
@@ -856,29 +909,49 @@
856
909
  callback(keyName, keyValue, otherCodeList, event);
857
910
  }
858
911
  };
859
- this.on(target, eventName, keyboardEventCallBack, options);
912
+ DOMUtilsContext.on(element, eventName, keyboardEventCallBack, options);
860
913
  return {
861
914
  removeListen: () => {
862
- this.off(target, eventName, keyboardEventCallBack, options);
915
+ DOMUtilsContext.off(element, eventName, keyboardEventCallBack, options);
863
916
  },
864
917
  };
865
918
  }
866
919
  }
867
920
 
921
+ /**
922
+ * 判断是否是元素列表
923
+ * @param $ele
924
+ */
925
+ const isNodeList = ($ele) => {
926
+ return Array.isArray($ele) || $ele instanceof NodeList;
927
+ };
868
928
  class DOMUtils extends DOMUtilsEvent {
869
929
  constructor(option) {
870
930
  super(option);
871
931
  }
872
932
  /** 版本号 */
873
- version = "2024.10.19";
933
+ version = "2024.10.22";
874
934
  attr(element, attrName, attrValue) {
875
935
  let DOMUtilsContext = this;
876
936
  if (typeof element === "string") {
877
- element = DOMUtilsContext.windowApi.document.querySelector(element);
937
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
878
938
  }
879
939
  if (element == null) {
880
940
  return;
881
941
  }
942
+ if (isNodeList(element)) {
943
+ if (attrValue == null) {
944
+ // 获取属性
945
+ return DOMUtilsContext.attr(element[0], attrName, attrValue);
946
+ }
947
+ else {
948
+ // 设置属性
949
+ element.forEach(($ele) => {
950
+ DOMUtilsContext.attr($ele, attrName, attrValue);
951
+ });
952
+ return;
953
+ }
954
+ }
882
955
  if (attrValue == null) {
883
956
  return element.getAttribute(attrName);
884
957
  }
@@ -967,14 +1040,40 @@
967
1040
  return propertyValue;
968
1041
  }
969
1042
  if (typeof element === "string") {
970
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1043
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
971
1044
  }
972
1045
  if (element == null) {
973
1046
  return;
974
1047
  }
1048
+ if (isNodeList(element)) {
1049
+ if (typeof property === "string") {
1050
+ if (value == null) {
1051
+ // 获取属性
1052
+ return DOMUtilsContext.css(element[0], property);
1053
+ }
1054
+ else {
1055
+ // 设置属性
1056
+ element.forEach(($ele) => {
1057
+ DOMUtilsContext.css($ele, property);
1058
+ });
1059
+ return;
1060
+ }
1061
+ }
1062
+ else if (typeof property === "object") {
1063
+ // 设置属性
1064
+ element.forEach(($ele) => {
1065
+ DOMUtilsContext.css($ele, property);
1066
+ });
1067
+ return;
1068
+ }
1069
+ else ;
1070
+ return;
1071
+ }
975
1072
  if (typeof property === "string") {
976
1073
  if (value == null) {
977
- return getComputedStyle(element).getPropertyValue(property);
1074
+ return DOMUtilsContext.windowApi.globalThis
1075
+ .getComputedStyle(element)
1076
+ .getPropertyValue(property);
978
1077
  }
979
1078
  else {
980
1079
  if (value === "string" && value.includes("!important")) {
@@ -998,15 +1097,29 @@
998
1097
  }
999
1098
  }
1000
1099
  }
1100
+ else ;
1001
1101
  }
1002
1102
  text(element, text) {
1003
1103
  let DOMUtilsContext = this;
1004
1104
  if (typeof element === "string") {
1005
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1105
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1006
1106
  }
1007
1107
  if (element == null) {
1008
1108
  return;
1009
1109
  }
1110
+ if (isNodeList(element)) {
1111
+ if (text == null) {
1112
+ // 获取
1113
+ return DOMUtilsContext.text(element[0]);
1114
+ }
1115
+ else {
1116
+ // 设置
1117
+ element.forEach(($ele) => {
1118
+ DOMUtilsContext.text($ele, text);
1119
+ });
1120
+ }
1121
+ return;
1122
+ }
1010
1123
  if (text == null) {
1011
1124
  return element.textContent || element.innerText;
1012
1125
  }
@@ -1025,15 +1138,30 @@
1025
1138
  html(element, html) {
1026
1139
  let DOMUtilsContext = this;
1027
1140
  if (typeof element === "string") {
1028
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1141
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1029
1142
  }
1030
1143
  if (element == null) {
1031
1144
  return;
1032
1145
  }
1146
+ if (isNodeList(element)) {
1147
+ if (html == null) {
1148
+ // 获取
1149
+ return DOMUtilsContext.html(element[0]);
1150
+ }
1151
+ else {
1152
+ // 设置
1153
+ element.forEach(($ele) => {
1154
+ DOMUtilsContext.html($ele, html);
1155
+ });
1156
+ }
1157
+ return;
1158
+ }
1033
1159
  if (html == null) {
1160
+ // 获取
1034
1161
  return element.innerHTML;
1035
1162
  }
1036
1163
  else {
1164
+ // 设置
1037
1165
  if (html instanceof Element) {
1038
1166
  html = html.innerHTML;
1039
1167
  }
@@ -1056,7 +1184,7 @@
1056
1184
  recovery();
1057
1185
  return transformInfo;
1058
1186
  }
1059
- let elementTransform = getComputedStyle(element).transform;
1187
+ let elementTransform = DOMUtilsContext.windowApi.globalThis.getComputedStyle(element).transform;
1060
1188
  if (elementTransform != null &&
1061
1189
  elementTransform !== "none" &&
1062
1190
  elementTransform !== "") {
@@ -1080,12 +1208,26 @@
1080
1208
  val(element, value) {
1081
1209
  let DOMUtilsContext = this;
1082
1210
  if (typeof element === "string") {
1083
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1211
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1084
1212
  }
1085
1213
  if (element == null) {
1086
1214
  return;
1087
1215
  }
1216
+ if (isNodeList(element)) {
1217
+ if (value == null) {
1218
+ // 获取
1219
+ return DOMUtilsContext.val(element[0]);
1220
+ }
1221
+ else {
1222
+ // 设置
1223
+ element.forEach(($ele) => {
1224
+ DOMUtilsContext.val($ele, value);
1225
+ });
1226
+ }
1227
+ return;
1228
+ }
1088
1229
  if (value == null) {
1230
+ // 获取
1089
1231
  if (element.localName === "input" &&
1090
1232
  (element.type === "checkbox" || element.type === "radio")) {
1091
1233
  return element.checked;
@@ -1095,6 +1237,7 @@
1095
1237
  }
1096
1238
  }
1097
1239
  else {
1240
+ // 设置
1098
1241
  if (element.localName === "input" &&
1099
1242
  (element.type === "checkbox" || element.type === "radio")) {
1100
1243
  element.checked = !!value;
@@ -1106,17 +1249,30 @@
1106
1249
  }
1107
1250
  prop(element, propName, propValue) {
1108
1251
  let DOMUtilsContext = this;
1252
+ if (typeof element === "string") {
1253
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1254
+ }
1109
1255
  if (element == null) {
1110
1256
  return;
1111
1257
  }
1112
- if (typeof element === "string") {
1113
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1258
+ if (isNodeList(element)) {
1259
+ if (propValue == null) {
1260
+ // 获取
1261
+ return DOMUtilsContext.prop(element[0], propName);
1262
+ }
1263
+ else {
1264
+ // 设置
1265
+ element.forEach(($ele) => {
1266
+ DOMUtilsContext.prop($ele, propName, propValue);
1267
+ });
1268
+ }
1269
+ return;
1114
1270
  }
1115
1271
  if (propValue == null) {
1116
- return element[propName];
1272
+ return Reflect.get(element, propName);
1117
1273
  }
1118
1274
  else {
1119
- element[propName] = propValue;
1275
+ Reflect.set(element, propName, propValue);
1120
1276
  }
1121
1277
  }
1122
1278
  /**
@@ -1131,11 +1287,18 @@
1131
1287
  removeAttr(element, attrName) {
1132
1288
  let DOMUtilsContext = this;
1133
1289
  if (typeof element === "string") {
1134
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1290
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1135
1291
  }
1136
1292
  if (element == null) {
1137
1293
  return;
1138
1294
  }
1295
+ if (isNodeList(element)) {
1296
+ // 设置
1297
+ element.forEach(($ele) => {
1298
+ DOMUtilsContext.removeAttr($ele, attrName);
1299
+ });
1300
+ return;
1301
+ }
1139
1302
  element.removeAttribute(attrName);
1140
1303
  }
1141
1304
  /**
@@ -1150,15 +1313,30 @@
1150
1313
  removeClass(element, className) {
1151
1314
  let DOMUtilsContext = this;
1152
1315
  if (typeof element === "string") {
1153
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1316
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1154
1317
  }
1155
1318
  if (element == null) {
1156
1319
  return;
1157
1320
  }
1158
- if (className == null) {
1321
+ if (isNodeList(element)) {
1322
+ // 设置
1323
+ element.forEach(($ele) => {
1324
+ DOMUtilsContext.removeClass($ele, className);
1325
+ });
1159
1326
  return;
1160
1327
  }
1161
- element.classList.remove(className);
1328
+ if (className == null) {
1329
+ // 清空全部className
1330
+ element.className = "";
1331
+ }
1332
+ else {
1333
+ if (!Array.isArray(className)) {
1334
+ className = className.split(" ");
1335
+ }
1336
+ className.forEach((itemClassName) => {
1337
+ element.classList.remove(itemClassName);
1338
+ });
1339
+ }
1162
1340
  }
1163
1341
  /**
1164
1342
  * 移除元素的属性
@@ -1172,11 +1350,18 @@
1172
1350
  removeProp(element, propName) {
1173
1351
  let DOMUtilsContext = this;
1174
1352
  if (typeof element === "string") {
1175
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1353
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1176
1354
  }
1177
1355
  if (element == null) {
1178
1356
  return;
1179
1357
  }
1358
+ if (isNodeList(element)) {
1359
+ // 设置
1360
+ element.forEach(($ele) => {
1361
+ DOMUtilsContext.removeProp($ele, propName);
1362
+ });
1363
+ return;
1364
+ }
1180
1365
  DOMUtilsCommonUtils.delete(element, propName);
1181
1366
  }
1182
1367
  /**
@@ -1191,22 +1376,22 @@
1191
1376
  replaceWith(element, newElement) {
1192
1377
  let DOMUtilsContext = this;
1193
1378
  if (typeof element === "string") {
1194
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1379
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1195
1380
  }
1196
1381
  if (element == null) {
1197
1382
  return;
1198
1383
  }
1199
- if (typeof newElement === "string") {
1200
- newElement = DOMUtilsContext.parseHTML(newElement, false, false);
1201
- }
1202
- if (element instanceof NodeList || element instanceof Array) {
1203
- element.forEach((item) => {
1204
- DOMUtilsContext.replaceWith(item, newElement);
1384
+ if (isNodeList(element)) {
1385
+ // 设置
1386
+ element.forEach(($ele) => {
1387
+ DOMUtilsContext.replaceWith($ele, newElement);
1205
1388
  });
1389
+ return;
1206
1390
  }
1207
- else {
1208
- element.parentElement.replaceChild(newElement, element);
1391
+ if (typeof newElement === "string") {
1392
+ newElement = DOMUtilsContext.parseHTML(newElement, false, false);
1209
1393
  }
1394
+ element.parentElement.replaceChild(newElement, element);
1210
1395
  }
1211
1396
  /**
1212
1397
  * 给元素添加class
@@ -1220,12 +1405,27 @@
1220
1405
  addClass(element, className) {
1221
1406
  let DOMUtilsContext = this;
1222
1407
  if (typeof element === "string") {
1223
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1408
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1224
1409
  }
1225
1410
  if (element == null) {
1226
1411
  return;
1227
1412
  }
1228
- element.classList.add(className);
1413
+ if (isNodeList(element)) {
1414
+ // 设置
1415
+ element.forEach(($ele) => {
1416
+ DOMUtilsContext.addClass($ele, className);
1417
+ });
1418
+ return;
1419
+ }
1420
+ if (!Array.isArray(className)) {
1421
+ className = className.split(" ");
1422
+ }
1423
+ className.forEach((itemClassName) => {
1424
+ if (itemClassName.trim() == "") {
1425
+ return;
1426
+ }
1427
+ element.classList.add(itemClassName);
1428
+ });
1229
1429
  }
1230
1430
  /**
1231
1431
  * 函数在元素内部末尾添加子元素或HTML字符串
@@ -1239,11 +1439,18 @@
1239
1439
  append(element, content) {
1240
1440
  let DOMUtilsContext = this;
1241
1441
  if (typeof element === "string") {
1242
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1442
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1243
1443
  }
1244
1444
  if (element == null) {
1245
1445
  return;
1246
1446
  }
1447
+ if (isNodeList(element)) {
1448
+ // 设置
1449
+ element.forEach(($ele) => {
1450
+ DOMUtilsContext.append($ele, content);
1451
+ });
1452
+ return;
1453
+ }
1247
1454
  function elementAppendChild(ele, text) {
1248
1455
  if (typeof content === "string") {
1249
1456
  ele.insertAdjacentHTML("beforeend", text);
@@ -1257,7 +1464,7 @@
1257
1464
  let fragment = DOMUtilsContext.windowApi.document.createDocumentFragment();
1258
1465
  content.forEach((ele) => {
1259
1466
  if (typeof ele === "string") {
1260
- ele = this.parseHTML(ele, true, false);
1467
+ ele = DOMUtilsContext.parseHTML(ele, true, false);
1261
1468
  }
1262
1469
  fragment.appendChild(ele);
1263
1470
  });
@@ -1279,11 +1486,18 @@
1279
1486
  prepend(element, content) {
1280
1487
  let DOMUtilsContext = this;
1281
1488
  if (typeof element === "string") {
1282
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1489
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1283
1490
  }
1284
1491
  if (element == null) {
1285
1492
  return;
1286
1493
  }
1494
+ if (isNodeList(element)) {
1495
+ // 设置
1496
+ element.forEach(($ele) => {
1497
+ DOMUtilsContext.prepend($ele, content);
1498
+ });
1499
+ return;
1500
+ }
1287
1501
  if (typeof content === "string") {
1288
1502
  element.insertAdjacentHTML("afterbegin", content);
1289
1503
  }
@@ -1309,11 +1523,18 @@
1309
1523
  after(element, content) {
1310
1524
  let DOMUtilsContext = this;
1311
1525
  if (typeof element === "string") {
1312
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1526
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1313
1527
  }
1314
1528
  if (element == null) {
1315
1529
  return;
1316
1530
  }
1531
+ if (isNodeList(element)) {
1532
+ // 设置
1533
+ element.forEach(($ele) => {
1534
+ DOMUtilsContext.after($ele, content);
1535
+ });
1536
+ return;
1537
+ }
1317
1538
  if (typeof content === "string") {
1318
1539
  element.insertAdjacentHTML("afterend", content);
1319
1540
  }
@@ -1341,11 +1562,18 @@
1341
1562
  before(element, content) {
1342
1563
  let DOMUtilsContext = this;
1343
1564
  if (typeof element === "string") {
1344
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1565
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1345
1566
  }
1346
1567
  if (element == null) {
1347
1568
  return;
1348
1569
  }
1570
+ if (isNodeList(element)) {
1571
+ // 设置
1572
+ element.forEach(($ele) => {
1573
+ DOMUtilsContext.before($ele, content);
1574
+ });
1575
+ return;
1576
+ }
1349
1577
  if (typeof content === "string") {
1350
1578
  element.insertAdjacentHTML("beforebegin", content);
1351
1579
  }
@@ -1361,30 +1589,28 @@
1361
1589
  }
1362
1590
  /**
1363
1591
  * 移除元素
1364
- * @param target 目标元素
1592
+ * @param element 目标元素
1365
1593
  * @example
1366
1594
  * // 元素a.xx前面添加一个元素
1367
1595
  * DOMUtils.remove(document.querySelector("a.xx"))
1368
1596
  * DOMUtils.remove(document.querySelectorAll("a.xx"))
1369
1597
  * DOMUtils.remove("a.xx")
1370
1598
  * */
1371
- remove(target) {
1599
+ remove(element) {
1372
1600
  let DOMUtilsContext = this;
1373
- if (typeof target === "string") {
1374
- target = DOMUtilsContext.windowApi.document.querySelectorAll(target);
1601
+ if (typeof element === "string") {
1602
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1375
1603
  }
1376
- if (target == null) {
1604
+ if (element == null) {
1377
1605
  return;
1378
1606
  }
1379
- if (target instanceof NodeList || target instanceof Array) {
1380
- target = target;
1381
- for (const element of target) {
1382
- DOMUtilsContext.remove(element);
1383
- }
1384
- }
1385
- else {
1386
- target.remove();
1607
+ if (isNodeList(element)) {
1608
+ element.forEach(($ele) => {
1609
+ DOMUtilsContext.remove($ele);
1610
+ });
1611
+ return;
1387
1612
  }
1613
+ element.remove();
1388
1614
  }
1389
1615
  /**
1390
1616
  * 移除元素的所有子元素
@@ -1397,11 +1623,18 @@
1397
1623
  empty(element) {
1398
1624
  let DOMUtilsContext = this;
1399
1625
  if (typeof element === "string") {
1400
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1626
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1401
1627
  }
1402
1628
  if (element == null) {
1403
1629
  return;
1404
1630
  }
1631
+ if (isNodeList(element)) {
1632
+ // 设置
1633
+ element.forEach(($ele) => {
1634
+ DOMUtilsContext.empty($ele);
1635
+ });
1636
+ return;
1637
+ }
1405
1638
  element.innerHTML = "";
1406
1639
  }
1407
1640
  /**
@@ -1432,7 +1665,8 @@
1432
1665
  width(element, isShow = false) {
1433
1666
  let DOMUtilsContext = this;
1434
1667
  if (typeof element === "string") {
1435
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1668
+ element =
1669
+ DOMUtilsContext.windowApi.document.querySelector(element);
1436
1670
  }
1437
1671
  if (element == null) {
1438
1672
  return;
@@ -1544,7 +1778,7 @@
1544
1778
  }
1545
1779
  element = element;
1546
1780
  if (isShow || (!isShow && DOMUtilsCommonUtils.isShow(element))) {
1547
- let style = getComputedStyle(element, null);
1781
+ let style = DOMUtilsContext.windowApi.globalThis.getComputedStyle(element, null);
1548
1782
  let marginLeft = DOMUtilsCommonUtils.getStyleValue(style, "marginLeft");
1549
1783
  let marginRight = DOMUtilsCommonUtils.getStyleValue(style, "marginRight");
1550
1784
  return element.offsetWidth + marginLeft + marginRight;
@@ -1570,7 +1804,7 @@
1570
1804
  }
1571
1805
  element = element;
1572
1806
  if (isShow || (!isShow && DOMUtilsCommonUtils.isShow(element))) {
1573
- let style = getComputedStyle(element, null);
1807
+ let style = DOMUtilsContext.windowApi.globalThis.getComputedStyle(element, null);
1574
1808
  let marginTop = DOMUtilsCommonUtils.getStyleValue(style, "marginTop");
1575
1809
  let marginBottom = DOMUtilsCommonUtils.getStyleValue(style, "marginBottom");
1576
1810
  return element.offsetHeight + marginTop + marginBottom;
@@ -1597,11 +1831,18 @@
1597
1831
  animate(element, styles, duration = 1000, callback = null) {
1598
1832
  let DOMUtilsContext = this;
1599
1833
  if (typeof element === "string") {
1600
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1834
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1601
1835
  }
1602
1836
  if (element == null) {
1603
1837
  return;
1604
1838
  }
1839
+ if (isNodeList(element)) {
1840
+ // 设置
1841
+ element.forEach(($ele) => {
1842
+ DOMUtilsContext.animate($ele, styles, duration, callback);
1843
+ });
1844
+ return;
1845
+ }
1605
1846
  if (typeof duration !== "number" || duration <= 0) {
1606
1847
  throw new TypeError("duration must be a positive number");
1607
1848
  }
@@ -1618,7 +1859,9 @@
1618
1859
  let from = {};
1619
1860
  let to = {};
1620
1861
  for (let prop in styles) {
1621
- from[prop] = element.style[prop] || getComputedStyle(element)[prop];
1862
+ from[prop] =
1863
+ element.style[prop] ||
1864
+ DOMUtilsContext.windowApi.globalThis.getComputedStyle(element)[prop];
1622
1865
  to[prop] = styles[prop];
1623
1866
  }
1624
1867
  let timer = setInterval(function () {
@@ -1650,18 +1893,26 @@
1650
1893
  wrap(element, wrapperHTML) {
1651
1894
  let DOMUtilsContext = this;
1652
1895
  if (typeof element === "string") {
1653
- element = DOMUtilsContext.windowApi.document.querySelector(element);
1896
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1654
1897
  }
1655
1898
  if (element == null) {
1656
1899
  return;
1657
1900
  }
1901
+ if (isNodeList(element)) {
1902
+ // 设置
1903
+ element.forEach(($ele) => {
1904
+ DOMUtilsContext.wrap($ele, wrapperHTML);
1905
+ });
1906
+ return;
1907
+ }
1658
1908
  element = element;
1659
1909
  // 创建一个新的div元素,并将wrapperHTML作为其innerHTML
1660
1910
  let wrapper = DOMUtilsContext.windowApi.document.createElement("div");
1661
1911
  wrapper.innerHTML = wrapperHTML;
1662
1912
  let wrapperFirstChild = wrapper.firstChild;
1663
1913
  // 将要包裹的元素插入目标元素前面
1664
- element.parentElement.insertBefore(wrapperFirstChild, element);
1914
+ let parentElement = element.parentElement;
1915
+ parentElement.insertBefore(wrapperFirstChild, element);
1665
1916
  // 将要包裹的元素移动到wrapper中
1666
1917
  wrapperFirstChild.appendChild(element);
1667
1918
  }
@@ -1741,6 +1992,8 @@
1741
1992
  }
1742
1993
  parseHTML(html, useParser = false, isComplete = false) {
1743
1994
  let DOMUtilsContext = this;
1995
+ // 去除html前后的空格
1996
+ html = html.trim();
1744
1997
  function parseHTMLByDOMParser() {
1745
1998
  let parser = new DOMParser();
1746
1999
  if (isComplete) {
@@ -1767,16 +2020,61 @@
1767
2020
  return parseHTMLByCreateDom();
1768
2021
  }
1769
2022
  }
2023
+ /**
2024
+ * 序列化表单元素
2025
+ * @param $form 表单元素
2026
+ * @example
2027
+ * DOMUtils.serialize(document.querySelector("form"))
2028
+ * > xxx=xxx&aaa=
2029
+ */
2030
+ serialize($form) {
2031
+ const elements = $form.elements;
2032
+ let serializedArray = [];
2033
+ for (let i = 0; i < elements.length; i++) {
2034
+ const element = elements[i];
2035
+ if (element.name &&
2036
+ !element.disabled &&
2037
+ (element.checked ||
2038
+ [
2039
+ "text",
2040
+ "hidden",
2041
+ "password",
2042
+ "textarea",
2043
+ "select-one",
2044
+ "select-multiple",
2045
+ ].includes(element.type))) {
2046
+ if (element.type === "select-multiple") {
2047
+ for (let j = 0; j < element.options.length; j++) {
2048
+ if (element.options[j].selected) {
2049
+ serializedArray.push({
2050
+ name: element.name,
2051
+ value: element.options[j].value,
2052
+ });
2053
+ }
2054
+ }
2055
+ }
2056
+ else {
2057
+ serializedArray.push({ name: element.name, value: element.value });
2058
+ }
2059
+ }
2060
+ }
2061
+ return serializedArray
2062
+ .map((item) => `${encodeURIComponent(item.name)}=${encodeURIComponent(item.value)}`)
2063
+ .join("&");
2064
+ }
1770
2065
  /**
1771
2066
  * 显示元素
1772
2067
  * @param target 当前元素
2068
+ * @param checkVisiblie 是否检测元素是否显示
2069
+ * + true (默认)如果检测到还未显示,则强制使用display: unset !important;
2070
+ * + false 不检测,直接设置display属性为空
1773
2071
  * @example
1774
2072
  * // 显示a.xx元素
1775
2073
  * DOMUtils.show(document.querySelector("a.xx"))
1776
2074
  * DOMUtils.show(document.querySelectorAll("a.xx"))
1777
2075
  * DOMUtils.show("a.xx")
1778
2076
  */
1779
- show(target) {
2077
+ show(target, checkVisiblie = true) {
1780
2078
  let DOMUtilsContext = this;
1781
2079
  if (target == null) {
1782
2080
  return;
@@ -1787,28 +2085,33 @@
1787
2085
  if (target instanceof NodeList || target instanceof Array) {
1788
2086
  target = target;
1789
2087
  for (const element of target) {
1790
- DOMUtilsContext.show(element);
2088
+ DOMUtilsContext.show(element, checkVisiblie);
1791
2089
  }
1792
2090
  }
1793
2091
  else {
1794
2092
  target = target;
1795
2093
  target.style.display = "";
1796
- if (!DOMUtilsCommonUtils.isShow(target)) {
1797
- /* 仍然是不显示,尝试使用强覆盖 */
1798
- target.style.setProperty("display", "unset", "important");
2094
+ if (checkVisiblie) {
2095
+ if (!DOMUtilsCommonUtils.isShow(target)) {
2096
+ /* 仍然是不显示,尝试使用强覆盖 */
2097
+ target.style.setProperty("display", "unset", "important");
2098
+ }
1799
2099
  }
1800
2100
  }
1801
2101
  }
1802
2102
  /**
1803
2103
  * 隐藏元素
1804
2104
  * @param target 当前元素
2105
+ * @param checkVisiblie 是否检测元素是否显示
2106
+ * + true (默认)如果检测到显示,则强制使用display: none !important;
2107
+ * + false 不检测,直接设置display属性为none
1805
2108
  * @example
1806
2109
  * // 隐藏a.xx元素
1807
2110
  * DOMUtils.hide(document.querySelector("a.xx"))
1808
2111
  * DOMUtils.hide(document.querySelectorAll("a.xx"))
1809
2112
  * DOMUtils.hide("a.xx")
1810
2113
  */
1811
- hide(target) {
2114
+ hide(target, checkVisiblie = true) {
1812
2115
  let DOMUtilsContext = this;
1813
2116
  if (target == null) {
1814
2117
  return;
@@ -1819,15 +2122,17 @@
1819
2122
  if (target instanceof NodeList || target instanceof Array) {
1820
2123
  target = target;
1821
2124
  for (const element of target) {
1822
- DOMUtilsContext.hide(element);
2125
+ DOMUtilsContext.hide(element, checkVisiblie);
1823
2126
  }
1824
2127
  }
1825
2128
  else {
1826
2129
  target = target;
1827
2130
  target.style.display = "none";
1828
- if (DOMUtilsCommonUtils.isShow(target)) {
1829
- /* 仍然是显示,尝试使用强覆盖 */
1830
- target.style.setProperty("display", "none", "important");
2131
+ if (checkVisiblie) {
2132
+ if (DOMUtilsCommonUtils.isShow(target)) {
2133
+ /* 仍然是显示,尝试使用强覆盖 */
2134
+ target.style.setProperty("display", "none", "important");
2135
+ }
1831
2136
  }
1832
2137
  }
1833
2138
  }
@@ -1851,9 +2156,15 @@
1851
2156
  }
1852
2157
  let DOMUtilsContext = this;
1853
2158
  if (typeof element === "string") {
1854
- element = DOMUtilsContext.windowApi.document.querySelector(element);
2159
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
2160
+ }
2161
+ if (isNodeList(element)) {
2162
+ // 设置
2163
+ element.forEach(($ele) => {
2164
+ DOMUtilsContext.fadeIn($ele, duration, callback);
2165
+ });
2166
+ return;
1855
2167
  }
1856
- element = element;
1857
2168
  element.style.opacity = "0";
1858
2169
  element.style.display = "";
1859
2170
  let start = null;
@@ -1896,9 +2207,15 @@
1896
2207
  return;
1897
2208
  }
1898
2209
  if (typeof element === "string") {
1899
- element = DOMUtilsContext.windowApi.document.querySelector(element);
2210
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
2211
+ }
2212
+ if (isNodeList(element)) {
2213
+ // 设置
2214
+ element.forEach(($ele) => {
2215
+ DOMUtilsContext.fadeOut($ele, duration, callback);
2216
+ });
2217
+ return;
1900
2218
  }
1901
- element = element;
1902
2219
  element.style.opacity = "1";
1903
2220
  let start = null;
1904
2221
  let timer = null;
@@ -1924,24 +2241,34 @@
1924
2241
  /**
1925
2242
  * 切换元素的显示和隐藏状态
1926
2243
  * @param element 当前元素
2244
+ * @param checkVisiblie 是否检测元素是否显示
1927
2245
  * @example
1928
2246
  * // 如果元素a.xx当前是隐藏,则显示,如果是显示,则隐藏
1929
2247
  * DOMUtils.toggle(document.querySelector("a.xx"))
1930
2248
  * DOMUtils.toggle("a.xx")
1931
2249
  */
1932
- toggle(element) {
2250
+ toggle(element, checkVisiblie) {
1933
2251
  let DOMUtilsContext = this;
1934
2252
  if (typeof element === "string") {
1935
- element = DOMUtilsContext.windowApi.document.querySelector(element);
2253
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
1936
2254
  }
1937
2255
  if (element == null) {
1938
2256
  return;
1939
2257
  }
1940
- if (getComputedStyle(element).getPropertyValue("display") === "none") {
1941
- DOMUtilsContext.show(element);
2258
+ if (isNodeList(element)) {
2259
+ // 设置
2260
+ element.forEach(($ele) => {
2261
+ DOMUtilsContext.toggle($ele);
2262
+ });
2263
+ return;
2264
+ }
2265
+ if (DOMUtilsContext.windowApi.globalThis
2266
+ .getComputedStyle(element)
2267
+ .getPropertyValue("display") === "none") {
2268
+ DOMUtilsContext.show(element, checkVisiblie);
1942
2269
  }
1943
2270
  else {
1944
- DOMUtilsContext.hide(element);
2271
+ DOMUtilsContext.hide(element, checkVisiblie);
1945
2272
  }
1946
2273
  }
1947
2274
  /**