• dom转换原生js对象


    参考类型:使用js将dom对象转换为js对象_37 degrees Celsius的博客-CSDN博客_js元素转对象

    直接吐出一个类型html标签:

    1. // 获取元素的属性,输出对象,如果没有属性输出null
    2. const getAttribute = (dom) => {
    3. let attributes = {};
    4. let empty = true;
    5. Array.from(dom.attributes).map(item => {
    6. attributes[item.nodeName] = item.nodeValue;
    7. empty = false;
    8. })
    9. return empty ? null : attributes;
    10. }
    11. // 将dom转为js-dom
    12. const domTrans = (dom) => {
    13. // 总dom树
    14. let tree = [];
    15. // 递归, node = 当前的节点,dataNode = 数据插入的节点
    16. const loop = (node, dataNode) => {
    17. // 当前节点的模板
    18. let temp = {
    19. nodeType: node.nodeType
    20. }
    21. // 如果是文本节点 或 单标签节点
    22. if (temp.nodeType == 3 && node.nodeValue.match(/\S/)) {
    23. temp['nodeValue'] = node.nodeValue;
    24. temp['nodeName'] = node.nodeName;
    25. temp['children']=[];
    26. dataNode.push(temp);
    27. }
    28. // 元素节点
    29. if (temp.nodeType == 1) {
    30. let attributes = getAttribute(node);
    31. // 如果没有属性,不添加 attributes
    32. if (attributes) temp['attributes'] = attributes;
    33. // 当前节点下只有文本 或 单标签节点
    34. if (node.childNodes.length <= 1) {
    35. temp['children']=[];
    36. temp['content'] = node.innerHTML;
    37. temp['nodeName'] = node.nodeName.toLowerCase();
    38. }
    39. // 当前节点下还有子节点
    40. if (node.childNodes.length > 1) {
    41. temp['children'] = [];
    42. temp['nodeName'] = node.nodeName.toLowerCase();
    43. for (let i = 0; i < node.childNodes.length; i++) {
    44. loop(node.childNodes[i], temp.children);
    45. }
    46. }
    47. dataNode.push(temp);
    48. }
    49. }
    50. loop(dom, tree);
    51. return tree[0].children;
    52. }
    53. console.log(
    54. domTrans(document.querySelector('html'))
    55. );

    这里边需要的属性要先定义。

    下边的生成的对象:

    1. {
    2. "nodeType": 9,
    3. "childNodes":[{
    4. "nodeType": 10,
    5. "nodeName": "html",
    6. "childNodes":[]
    7. },{
    8. "nodeType": 1,
    9. "nodeName": "html",
    10. "childNodes":[
    11. {
    12. "nodeType": 1,
    13. "childNodes": [
    14. {
    15. "nodeType": 1,
    16. "attributes": {
    17. "http-equiv": "Content-Type",
    18. "content": "text/html; charset=UTF-8"
    19. },
    20. "content": "",
    21. "nodeName": "meta"
    22. },
    23. {
    24. "nodeType": 1,
    25. "attributes": {
    26. "name": "viewport",
    27. "content": "width=device-width, initial-scale=1"
    28. },
    29. "content": "",
    30. "nodeName": "meta"
    31. },
    32. {
    33. "nodeType": 1,
    34. "content": "滑动模式",
    35. "nodeName": "title"
    36. },
    37. {
    38. "nodeType": 1,
    39. "content": "\n body {\n margin: 50px 0;\n text-align: center;\n font-family: \"PingFangSC-Regular\", \"Open Sans\", Arial, \"Hiragino Sans GB\", \"Microsoft YaHei\", \"STHeiti\", \"WenQuanYi Micro Hei\", SimSun, sans-serif;\n }\n\n .inp {\n border: 1px solid #cccccc;\n border-radius: 2px;\n padding: 0 10px;\n width: 278px;\n height: 40px;\n font-size: 18px;\n }\n\n .btn {\n display: inline-block;\n box-sizing: border-box;\n border: 1px solid #cccccc;\n border-radius: 2px;\n width: 100px;\n height: 40px;\n line-height: 40px;\n font-size: 16px;\n color: #666;\n cursor: pointer;\n background: white linear-gradient(180deg, #ffffff 0%, #f3f3f3 100%);\n }\n\n .btn:hover {\n background: white linear-gradient(0deg, #ffffff 0%, #f3f3f3 100%)\n }\n\n #captcha {\n width: 300px;\n display: inline-block;\n }\n\n label {\n vertical-align: top;\n display: inline-block;\n width: 80px;\n text-align: right;\n }\n\n #text {\n height: 42px;\n width: 298px;\n text-align: center;\n border-radius: 2px;\n background-color: #F3F3F3;\n color: #BBBBBB;\n font-size: 14px;\n letter-spacing: 0.1px;\n line-height: 42px;\n }\n\n #wait {\n display: none;\n height: 42px;\n width: 298px;\n text-align: center;\n border-radius: 2px;\n background-color: #F3F3F3;\n }\n\n .loading {\n margin: auto;\n width: 70px;\n height: 20px;\n }\n\n .loading-dot {\n float: left;\n width: 8px;\n height: 8px;\n margin: 18px 4px;\n background: #ccc;\n\n -webkit-border-radius: 50%;\n -moz-border-radius: 50%;\n border-radius: 50%;\n\n opacity: 0;\n\n -webkit-box-shadow: 0 0 2px black;\n -moz-box-shadow: 0 0 2px black;\n -ms-box-shadow: 0 0 2px black;\n -o-box-shadow: 0 0 2px black;\n box-shadow: 0 0 2px black;\n\n -webkit-animation: loadingFade 1s infinite;\n -moz-animation: loadingFade 1s infinite;\n animation: loadingFade 1s infinite;\n }\n\n .loading-dot:nth-child(1) {\n -webkit-animation-delay: 0s;\n -moz-animation-delay: 0s;\n animation-delay: 0s;\n }\n\n .loading-dot:nth-child(2) {\n -webkit-animation-delay: 0.1s;\n -moz-animation-delay: 0.1s;\n animation-delay: 0.1s;\n }\n\n .loading-dot:nth-child(3) {\n -webkit-animation-delay: 0.2s;\n -moz-animation-delay: 0.2s;\n animation-delay: 0.2s;\n }\n\n .loading-dot:nth-child(4) {\n -webkit-animation-delay: 0.3s;\n -moz-animation-delay: 0.3s;\n animation-delay: 0.3s;\n }\n\n @-webkit-keyframes loadingFade {\n 0% { opacity: 0; }\n 50% { opacity: 0.8; }\n 100% { opacity: 0; }\n }\n\n @-moz-keyframes loadingFade {\n 0% { opacity: 0; }\n 50% { opacity: 0.8; }\n 100% { opacity: 0; }\n }\n\n @keyframes loadingFade {\n 0% { opacity: 0; }\n 50% { opacity: 0.8; }\n 100% { opacity: 0; }\n }\n ",
    40. "nodeName": "style"
    41. },
    42. {
    43. "nodeType": 1,
    44. "attributes": {
    45. "charset": "UTF-8",
    46. "async": "",
    47. "src": "./滑动模式_files/gettype.php"
    48. },
    49. "content": "",
    50. "nodeName": "script"
    51. },
    52. {
    53. "nodeType": 1,
    54. "attributes": {
    55. "charset": "UTF-8",
    56. "async": "",
    57. "crossorigin": "anonymous",
    58. "src": "./滑动模式_files/fullpage.9.1.0.js.下载"
    59. },
    60. "content": "",
    61. "nodeName": "script"
    62. }
    63. ],
    64. "nodeName": "head"
    65. },
    66. {
    67. "nodeType": 1,
    68. "childNodes": [
    69. {
    70. "nodeType": 1,
    71. "nodeName": "h2"
    72. },
    73. {
    74. "nodeType": 1,
    75. "content": "滑动模式",
    76. "nodeName": "h1"
    77. },
    78. {
    79. "nodeType": 1,
    80. "attributes": {
    81. "id": "form"
    82. },
    83. "childNodes": [
    84. {
    85. "nodeType": 1,
    86. "childNodes": [
    87. {
    88. "nodeType": 1,
    89. "attributes": {
    90. "for": "username"
    91. },
    92. "content": "用户名:",
    93. "nodeName": "label"
    94. },
    95. {
    96. "nodeType": 1,
    97. "attributes": {
    98. "class": "inp",
    99. "id": "username",
    100. "type": "text",
    101. "value": "用户名"
    102. },
    103. "content": "",
    104. "nodeName": "input"
    105. }
    106. ],
    107. "nodeName": "div"
    108. },
    109. {
    110. "nodeType": 1,
    111. "content": "",
    112. "nodeName": "br"
    113. },
    114. {
    115. "nodeType": 1,
    116. "childNodes": [
    117. {
    118. "nodeType": 1,
    119. "attributes": {
    120. "for": "password"
    121. },
    122. "content": "密码:",
    123. "nodeName": "label"
    124. },
    125. {
    126. "nodeType": 1,
    127. "attributes": {
    128. "class": "inp",
    129. "id": "password",
    130. "type": "password",
    131. "value": "123456"
    132. },
    133. "content": "",
    134. "nodeName": "input"
    135. }
    136. ],
    137. "nodeName": "div"
    138. },
    139. {
    140. "nodeType": 1,
    141. "content": "",
    142. "nodeName": "br"
    143. },
    144. {
    145. "nodeType": 1,
    146. "childNodes": [
    147. {
    148. "nodeType": 1,
    149. "content": "完成验证:",
    150. "nodeName": "label"
    151. },
    152. {
    153. "nodeType": 1,
    154. "attributes": {
    155. "id": "captcha"
    156. },
    157. "childNodes": [
    158. {
    159. "nodeType": 1,
    160. "attributes": {
    161. "id": "text",
    162. "style": "display: none;"
    163. },
    164. "content": "\n 行为验证™ 安全组件加载中\n ",
    165. "nodeName": "div"
    166. },
    167. {
    168. "nodeType": 1,
    169. "attributes": {
    170. "id": "wait",
    171. "class": "show",
    172. "style": "display: block;"
    173. },
    174. "childNodes": [
    175. {
    176. "nodeType": 1,
    177. "attributes": {
    178. "class": "loading"
    179. },
    180. "childNodes": [
    181. {
    182. "nodeType": 1,
    183. "attributes": {
    184. "class": "loading-dot"
    185. },
    186. "content": "",
    187. "nodeName": "div"
    188. },
    189. {
    190. "nodeType": 1,
    191. "attributes": {
    192. "class": "loading-dot"
    193. },
    194. "content": "",
    195. "nodeName": "div"
    196. },
    197. {
    198. "nodeType": 1,
    199. "attributes": {
    200. "class": "loading-dot"
    201. },
    202. "content": "",
    203. "nodeName": "div"
    204. },
    205. {
    206. "nodeType": 1,
    207. "attributes": {
    208. "class": "loading-dot"
    209. },
    210. "content": "",
    211. "nodeName": "div"
    212. }
    213. ],
    214. "nodeName": "div"
    215. }
    216. ],
    217. "nodeName": "div"
    218. }
    219. ],
    220. "nodeName": "div"
    221. }
    222. ],
    223. "nodeName": "div"
    224. },
    225. {
    226. "nodeType": 1,
    227. "content": "",
    228. "nodeName": "br"
    229. },
    230. {
    231. "nodeType": 1,
    232. "attributes": {
    233. "id": "btn",
    234. "class": "btn"
    235. },
    236. "content": "提交",
    237. "nodeName": "div"
    238. }
    239. ],
    240. "nodeName": "form"
    241. },
    242. {
    243. "nodeType": 1,
    244. "attributes": {
    245. "src": "./滑动模式_files/jquery.js.下载"
    246. },
    247. "content": "",
    248. "nodeName": "script"
    249. },
    250. {
    251. "nodeType": 1,
    252. "attributes": {
    253. "src": "./滑动模式_files/gt.js.下载"
    254. },
    255. "content": "",
    256. "nodeName": "script"
    257. },
    258. {
    259. "nodeType": 1,
    260. "content": "\n\n\n var handler = function (captchaObj) {\n captchaObj.appendTo('#captcha');\n captchaObj.onReady(function () {\n $(\"#wait\").hide();\n });\n $('#btn').click(function () {\n var result = captchaObj.getValidate();\n if (!result) {\n return alert('请完成验证');\n }\n $.ajax({\n url: 'gt/validate-slide',\n type: 'POST',\n dataType: 'json',\n data: {\n username: $('#username2').val(),\n password: $('#password2').val(),\n geetest_challenge: result.geetest_challenge,\n geetest_validate: result.geetest_validate,\n geetest_seccode: result.geetest_seccode\n },\n success: function (data) {\n if (data.status === 'success') {\n alert('登录成功');\n } else if (data.status === 'fail') {\n alert('登录失败,请完成验证');\n captchaObj.reset();\n }\n }\n });\n })\n // 更多接口说明请参见:http://docs.geetest.com/install/client/web-front/\n window.gt = captchaObj;\n };\n\n\n $.ajax({\n url: \"gt/register-slide?t=\" + (new Date()).getTime(), // 加随机数防止缓存\n type: \"get\",\n dataType: \"json\",\n success: function (data) {\n\n $('#text').hide();\n $('#wait').show();\n // 调用 initGeetest 进行初始化\n // 参数1:配置参数\n // 参数2:回调,回调的第一个参数验证码对象,之后可以使用它调用相应的接口\n initGeetest({\n // 以下 4 个配置参数为必须,不能缺少\n gt: data.gt,\n challenge: data.challenge,\n offline: !data.success, // 表示用户后台检测极验服务器是否宕机\n new_captcha: data.new_captcha, // 用于宕机时表示是新验证码的宕机\n\n product: \"float\", // 产品形式,包括:float,popup\n width: \"300px\",\n https: true,\n api_server: \"apiv6.geetest.com\"\n\n // 更多配置参数说明请参见:http://docs.geetest.com/install/client/web-front/\n }, handler);\n }\n });\n",
    261. "nodeName": "script"
    262. }
    263. ],
    264. "nodeName": "body"
    265. }
    266. ]
    267. }],
    268. "nodeName":'#document',
    269. }

  • 相关阅读:
    学习FPGA之五:工具链
    SQL Server Management Studio (SSMS) 20.1 - 微软数据库管理工具
    微信小程序使用canvas绘图,圆形头像,网络背景图,文字,虚线,直线
    【云原生 • Kubernetes】搭建 k8s 集群(Kubeadm 方式)
    Grafana9.0发布,Prometheus和Loki查询生成器、全新导航、热图面板等新功能!
    测试22222
    【达摩院OpenVI】视频目标渐进式Transformer跟踪器ProContEXT
    浅析 Redis 中 String 数据类型及其底层编码
    JS中的构造函数
    关于ITSS认证资质整改和降级
  • 原文地址:https://blog.csdn.net/fan13938409755/article/details/126968663