當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JavaScript创建Element元素/标签的工具/方法
生活随笔
收集整理的這篇文章主要介紹了
JavaScript创建Element元素/标签的工具/方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
傳入注釋中的格式的對象即可創(chuàng)建一個Element,如果有好的建議可以寫在評論中。
/* 使用myCreateElementUtil函數(shù)時config形參格式如下 var config = {eleName:"a",eleStyle:{"width":"10px","height":"10px"},eleAttributes:{"name":"test","id":"t1"},eleText:"click me" } */ function myCreateElementUtil(config) {var ele = document.createElement(config.eleName);for(var attribute in config.eleAttributes) {ele.setAttribute(attribute,config.eleAttributes[attribute]);}for(var style in config.eleStyle) {ele.style[style] = config.eleStyle[style];}if(config.eleText != null && config.eleText != undefined) {var textNode = document.createTextNode(config.eleText);ele.appendChild(textNode);}return ele; }總結(jié)
以上是生活随笔為你收集整理的JavaScript创建Element元素/标签的工具/方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。