要从html页面获取元素,可以使用DOM方法,例如.getElementById()
。此方法返回一个Javascript对象,其中包含元素属性的详尽列表。以下示例来自MDN文档中的页面,侧重于id="Syntax"
的标头。
Here is the link.
*此列表的格式不正确,因为我是从浏览器控制台复制的。
> 17:38:20.198 h2#SyntaxaccessKey:"" accessKeyLabel:"" align:""属性:NamedNodeMap [id ="语法&#34 ;,name ="语法" ] baseURI:" https://developer.mozilla.org/en-US/docs/Web/API/Element/attributes" childElementCount:1childNodes:NodeList [#text,a.button.section-edit.only-icon] children:HTMLCollection [a.button.section-edit.only -icon] classList:DOMTokenList [] className:"" clientHeight:48clientLeft:0clientTop:0clientWidth:1019contentEditable:" inherit" contextMenu:nulldataset:DOMStringMap {} dir:"& #34; draggable:falsefirstChild:#text"语法" firstElementChild:hidden:falseid:"语法" innerHTML:"语法编辑" innerText:"语法\ n编辑& #34; isConnected:trueisContentEditable:falselang:"" lastChild:lastElementChild:localName:" h2" namespaceURI:" http://www.w3.org/1999/xhtml" nextElementSibling:nextSibling: #text" "节点名称:#34; H 2"节点类型:1nodeValue:nulloffsetHeight:48offsetLeft:0offsetParent:的offsetTop:231offsetWidth:1019onabort:nullonanimationcancel:nullonanimationend:nullonanimationiteration:nullonanimationstart:nullonauxclick:nullonblur:nulloncanplay:nulloncanplaythrough:nullonchange:nullonclick: nullonclose:nulloncontextmenu:nulloncopy:nulloncut:nullondblclick:nullondrag:nullondragend:nullondragenter:nullondragexit:nullondragleave:nullondragover:nullondragstart:nullondrop:nullondurationchange:nullonemptied:nullonended:nullonerror:nullonfocus:nulloninput:nulloninvalid:nullonkeydown:nullonkeypress:nullonkeyup:nullonload:nullonloadeddata: nullonloadedmetadata:nullonloadend:nullonloadstart:nullonmousedown:nullonmouseenter:nullonmouseleave:nullonmousemove:nullonmouseout:nullonmouseover:nullonmouseup:nullonmozfullscreenchange:nullonmozfullscreenerror:nullonpaste:nullonpause:nullonplay:nullonplaying:nullonprogress:nullonratechange :nullonreset:nullonresize:nullonscroll:nullonseeked:nullonseeking:nullonselect:nullonselectstart:nullonshow:nullonstalled:nullonsubmit:nullonsuspend:nullontimeupdate:nullontoggle:nullontransitioncancel:nullontransitionend:nullontransitionrun:nullontransitionstart:nullonvolumechange:nullonwaiting:nullonwebkitanimationend:nullonwebkitanimationiteration:nullonwebkitanimationstart:nullonwebkittransitionend:nullonwheel:nullouterHTML :" SyntaxEdit" ownerDocument:HTMLDocument https://developer.mozilla.org/en-US/docs/Web/API/Element/attributesparentElement:parentNode:prefix:nullpreviousElementSibling:
previousSibling:#text" " scrollHeight:49scrollLeft:0scrollLeftMax:0scrollTop:0scrollTopMax:0scrollWidth:1019spellcheck:falsestyle:CSS2Properties {} tabIndex:-1tagName:" H2" textContent:" SyntaxEdit" title:&# 34;" proto :HTMLHeadingElementPrototype {align:Getter&塞特,......}
使用此列表,是否可以创建html元素?
答案 0 :(得分:1)
你似乎不明白你所看到的是什么。任何querySelector类型的js方法做都会返回对内存中DOM元素的直接引用,而不仅仅是属性列表。您查看的所有console.log()输出都是为了您的方便。所以,它已经是一个html元素。
如果要创建类型的新元素,请使用document.createElement('type')
。如果您希望创建现有元素/节点的副本,请使用cloneNode
https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode。