From e83a44170a83bcd0e7228c89a89f2184235c9a1e Mon Sep 17 00:00:00 2001 From: JMRY <376509849@qq.com> Date: Thu, 28 Dec 2023 16:37:18 +0800 Subject: [PATCH] =?UTF-8?q?-=20Version:=201.0.9=20Build=2020231228=20=09-?= =?UTF-8?q?=20=E5=B0=86$=E6=9B=B4=E5=90=8D=E4=B8=BAjQuery=E4=BB=A5?= =?UTF-8?q?=E9=80=82=E9=85=8D=E6=9B=B4=E5=A4=9A=E5=9C=BA=E5=90=88=E3=80=82?= =?UTF-8?q?=20=09-=20=E5=8A=A0=E5=85=A5=E5=8E=9F=E7=94=9FgetDOMHtml?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 66 +++-- VERSION.md | 3 + html.dom.js | 143 ++++++++++ jquery.extensions.dom.js | 549 ++++++++++++++++++++------------------- 4 files changed, 452 insertions(+), 309 deletions(-) create mode 100644 html.dom.js diff --git a/README.md b/README.md index 290f4d2..6c3f120 100644 --- a/README.md +++ b/README.md @@ -22,11 +22,22 @@ #### 获取JQueryDOM HTML +- 此方法仅为获取HTML字符串,无法绑定事件。 + - ```javascript $.getDOMHtml(`div`,{id:`div`,class:`div`},`This is a DIV.`); $.getHtml(`div`,{id:`div`,class:`div`},`This is a DIV.`); ``` +#### 原生方式获取JQueryDOM HTML + +- 需引入html.dom.js脚本。 + +- 此方法仅为获取HTML字符串,无法绑定事件。 + +- ```javascript + getDOMHtml(`div`,{id:`div`,class:`div`},`This is a DIV.`); + ``` #### 插入元素 @@ -159,18 +170,22 @@ - 表格元素拥有特殊的语法,顶层使用tbody或tr取代children,tr中使用td取代children,并且可省略tag。 +- 表格只建议使用单层对象写法。 + +- 可用tbody替代tr。 + - ```javascript $(`body`).appendDOM(`table`,{ - id:`testTable`,class:`testTable`,tbody:[ - {attr:{id:`tr1`,class:`tr1`},td:[ - {attr:{id:`td1`,class:`td1`,html:`test td 1`}}, - {attr:{id:`td2`,class:`td2`},html:`test td 2`}, + id:`testTable`,class:`testTable`,tr:[ + {id:`tr1`,class:`tr1`,td:[ + {id:`td1`,class:`td1`,html:`test td 1`}, + {id:`td2`,class:`td2`,html:`test td 2`}, {html:`test td 3`}, `test td 4`, ]}, {td:[ - {attr:{id:`td1`,class:`td1`,html:`test td 31`}}, - {attr:{id:`td2`,class:`td2`},html:`test td 32`}, + {id:`td1`,class:`td1`,html:`test td 31`}, + {id:`td2`,class:`td2`,html:`test td 32`}, {html:`test td 33`}, `test td 34`, ]}, @@ -182,18 +197,18 @@ - ```javascript $(`body`).appendDOM(`table`,{ - id:`testTable`,class:`testTable`,tbody:[ - {attr:{id:`tr1`,class:`tr1`},td:[ - {attr:{id:`td1`,class:`td1`,children:[ - {attr:{id:`tdiv1`,class:`tdiv1`},html:`test td div`}, - ]}}, - {attr:{},html:`test td 2`}, + id:`testTable`,class:`testTable`,tr:[ + {id:`tr1`,class:`tr1`,td:[ + {id:`td1`,class:`td1`,children:[ + {tag:`div`,attr:{id:`tdiv1`,class:`tdiv1`},html:`test td div`}, + ]}, + {html:`test td 2`}, {html:`test td 3`}, `test td 4`, ]}, {td:[ - {attr:{id:`td1`,class:`td1`,html:`test td 31`}}, - {attr:{id:`td2`,class:`td2`},html:`test td 32`}, + {id:`td1`,class:`td1`,html:`test td 31`}, + {id:`td2`,class:`td2`,html:`test td 32`}, {html:`test td 33`}, `test td 34`, ]}, @@ -201,30 +216,9 @@ }); ``` -- 可用tr替代tbody。 - -- 支持单层对象写法。 +- 对象参数写法: - ```javascript - $(`body`).appendDOM(`table`,{ - id:`testTable`,class:`testTable`,tr:[ - {id:`tr1`,class:`tr1`,td:[ - {id:`td1`,class:`td1`,html:`test td 1`}, - {children:[ - {id:`tdiv1`,class:`tdiv1`,html:`test td div`}, - ]}, - {html:`test td 3`}, - `test td 4`, - ]}, - {td:[ - {id:`td1`,class:`td1`,html:`test td 31`}, - {id:`td2`,class:`td2`,html:`test td 32`}, - {html:`test td 33`}, - `test td 34`, - ]}, - ], - }); - // 对象参数写法: $(`body`).appendDOM({ tag:`table`,id:`testTable`,class:`testTable`,tr:[ {id:`tr1`,class:`tr1`,td:[ diff --git a/VERSION.md b/VERSION.md index c2b2c8f..5be04dc 100644 --- a/VERSION.md +++ b/VERSION.md @@ -1,4 +1,7 @@ # JQuery DOM +- Version: 1.0.9 Build 20231228 + - 将$更名为jQuery以适配更多场合。 + - 加入原生getDOMHtml功能。 - Version: 1.0.9 Build 20231227 - 将getHtml更名为getDOMHtml。 - 加入表格使用tr替代tbody功能。 diff --git a/html.dom.js b/html.dom.js new file mode 100644 index 0000000..96dd421 --- /dev/null +++ b/html.dom.js @@ -0,0 +1,143 @@ +getDOMHtml=function(dom_tag,dom_attr,dom_html){ + let domFullHtml=[]; + //dom_tag为数组时,批量为母元素添加元素 + if(typeof dom_tag==`object` && dom_tag.length!=undefined){ + let default_children={ + tag:undefined,attr:undefined,html:undefined,attachType:`append` + }; + for(let cur of dom_tag){ + cur={ + ...JSON.parse(JSON.stringify(default_children)), + ...cur, + } + domFullHtml.push(getDOMHtml(cur)); + } + return domFullHtml.join(``); + } + + //dom_tag为对象时,和普通情况一样 + if(typeof dom_tag==`object` && dom_tag.length==undefined){ + let dom_attr_fix_blacklist=[ + `tag`,`attachType`, + ] + let dom_attr_fix_replace={ + tagName:`tag`, attrName:`attr`, + } + let dom_attr_fix={}; + if(dom_tag.attr==undefined){ + for(let key in dom_tag){ + if(!dom_attr_fix_blacklist.includes(key)){ + let key_fix=key; + for(let origin in dom_attr_fix_replace){ + key_fix=key_fix.replace(origin,dom_attr_fix_replace[origin]); + } + dom_attr_fix[key_fix]=dom_tag[key]; + } + } + } + dom_attr=dom_tag.attr || dom_attr_fix; + dom_html=dom_attr.html || dom_tag.html; + dom_tag=dom_tag.tag; + } + + if(typeof dom_attr==`object`){ + if(typeof dom_attr.class==`object` && dom_attr.class.length){ + dom_attr.class=dom_attr.class.join(` `); + } + if(typeof dom_attr.style==`object`){ + let styleList=[]; + for(let key in dom_attr.style){ + styleList.push(`${key.replace(/([A-Z])/g,"-$1").toLowerCase()}: ${dom_attr.style[key]}`); + } + dom_attr.style=styleList.join(`;`); + } + } + + let attr_blacklist=[ + `bind`,`children`,`html`,`tbody`,`tr`,`td`, + ] + let domElement=document.createElement(dom_tag); + if(typeof dom_attr==`object`){ + for(let key in dom_attr){ + if(!attr_blacklist.includes(key)){ + domElement.setAttribute(key, dom_attr[key]); + } + } + }else if(typeof dom_attr==`string`){ + domFullHtml.push(dom_attr); + } + if(dom_html){ + domFullHtml.push(dom_html); + } + + if(typeof dom_attr==`object` && typeof dom_attr.children==`object`){ + let default_children={ + tag:undefined,attr:undefined,html:undefined, + }; + + if(dom_attr.children.length==undefined){ + /*仅一个子项时,可以直接使用Object + { + tag:`html`,attr:{id:`id`},html:`Test`,attachType:`append` + } + */ + let children={ + ...JSON.parse(JSON.stringify(default_children)), + ...dom_attr.children, + } + domFullHtml.push(getDOMHtml(children)); + }else{ + /*多个子项时,采用数组形式 + [ + { + tag:`html`,attr:{id:`id1`},html:`Test1`,attachType:`append` + }, + { + tag:`html`,attr:{id:`id2`},html:`Test2`,attachType:`append` + }, + ] + */ + for(let i=0; i${dom_html}`; } - return `<${dom_tag}${dom_attr_string}>${dom_html}`; -} - -$.getDOMObject=function(dom_tag,dom_attr,dom_html){ - try{ - let domObject=$($.getDOMString(dom_tag, dom_attr, dom_html)); - if(typeof dom_attr==`object`){ - //DOM样式 - try{ - /* - CSS Struct: - style:{ - width:`255px`, - height:`255px`, - } - */ - if(typeof dom_attr.style==`object`){ - domObject.css(dom_attr.style); + jQuery.getDOMObject=function(dom_tag,dom_attr,dom_html){ + try{ + let domObject=jQuery(jQuery.getDOMString(dom_tag, dom_attr, dom_html)); + if(typeof dom_attr==`object`){ + //DOM样式 + try{ + /* + CSS Struct: + style:{ + width:`255px`, + height:`255px`, + } + */ + if(typeof dom_attr.style==`object`){ + domObject.css(dom_attr.style); + } + }catch(e){ + console.error(e); } - }catch(e){ - console.error(e); - } - //DOM事件绑定 - try{ - /* - Bind Struct: - bind:{ - click:function, - } - Another Struct: - bind:{ - click:{ - data:{}, - function:function, + //DOM事件绑定 + try{ + /* + Bind Struct: + bind:{ + click:function, } - } - */ - if(typeof dom_attr.bind==`object`){ - for(let key in dom_attr.bind){ - let curBind=dom_attr.bind[key]; - domObject.unbind(key); - if(typeof curBind==`function`){ - domObject.bind(key, curBind); - }else if(typeof curBind==`object`){ - curBind={ - ...{ - data:{}, - function(){}, - }, - ...curBind, + Another Struct: + bind:{ + click:{ + data:{}, + function:function, + } + } + */ + if(typeof dom_attr.bind==`object`){ + for(let key in dom_attr.bind){ + let curBind=dom_attr.bind[key]; + domObject.unbind(key); + if(typeof curBind==`function`){ + domObject.bind(key, curBind); + }else if(typeof curBind==`object`){ + curBind={ + ...{ + data:{}, + function(){}, + }, + ...curBind, + } + domObject.bind(key, curBind.data, curBind.function); } - domObject.bind(key, curBind.data, curBind.function); } } + }catch(e){ + console.error(e); } - }catch(e){ - console.error(e); - } - //DOM子项 - try{ - if(typeof dom_attr.children==`object`){ - let default_children={ - tag:undefined,attr:undefined,html:undefined,attachType:`append` - }; + //DOM子项 + try{ + if(typeof dom_attr.children==`object`){ + let default_children={ + tag:undefined,attr:undefined,html:undefined,attachType:`append` + }; - if(dom_attr.children.length==undefined){ - /*仅一个子项时,可以直接使用Object - { - tag:`html`,attr:{id:`id`},html:`Test`,attachType:`append` - } - */ - let children={ - // ...default_children, - ...JSON.parse(JSON.stringify(default_children)), - ...dom_attr.children, - } - // domObject.attachDOM(children.tag,children.attr,children.html,children.attachType); - domObject.attachDOM(children); - }else{ - /*多个子项时,采用数组形式 - [ - { - tag:`html`,attr:{id:`id1`},html:`Test1`,attachType:`append` - }, + if(dom_attr.children.length==undefined){ + /*仅一个子项时,可以直接使用Object { - tag:`html`,attr:{id:`id2`},html:`Test2`,attachType:`append` - }, - ] - */ - for(let i=0; i