请选择 进入手机版 | 继续访问电脑版

技术控

    今日:17| 主题:61562
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] docrel — Slightly better document.createElement

[复制链接]
放弃治疗 发表于 2016-10-11 22:50:55
136 2
docrel         

  Slightly better document.createElement
      Docrelis a thin wrapper for    document.createElementthat makes creating elements a bit easier. It also helps clean up your code and avoid repetition. No dependencies, no black magic (seesource).  
  Using    document.createElement:  
  1. let select = document.createElement("select")
  2. select.classList.add("class-1", "class-2")
  3. let option = document.createElement("option")
  4. option.setAttribute("value", someVar)
  5. if (someVar === selectedVar) {
  6.   node.setAttribute("selected", "selected")
  7. }
  8. node.textContent = `This is ${anotherVar}`
复制代码
Using    docrel:  
  1. import {createElement} from "docrel"
  2. let select = createElement("select", {classList: ["class-1", "class-2"]})
  3. let option = createElement("option", {
  4.   attrs: {
  5.     value: someVar,
  6.     selected: someVar === selectedVar ? "selected" : null
  7.   },
  8.   textContent: `This is ${anotherVar}`
  9. })
复制代码
If    someVar === selectedVarreturns    nullthe attribute won't be set at all.  
  Install

  1. npm install docrel --save
复制代码
Usage

  1. import {createElement} from "docrel"
  2. let el = createElement("div", {
  3.   textContent: "Text",
  4.   classList: ["array", "of", "classes"],
  5.   attrs: {
  6.     align: "center",
  7.     "data-attr": 1
  8.   },
  9.   events: {
  10.     click: () => {console.log("click!")}
  11.   }
  12. }
复制代码

       
  • Keys inside      attrsare passed to      node.setAttribute, unless key value is      nullor      undefined;   
  • Keys inside      eventsare passed to      node.addEventListener;   
  • Returns an HTML Element object.  
  About

  This project is sponsored by    Heresy. We're making an innovative tool for enterprise sales and we're building a team of engineers here in London (UK) who love open source and JavaScript. If you would like to hear more, just get in touch via email:    team [at] heresy [dot] i/o.  
  Contributing

  Contributions are more than welcome. Please fork the repo and send a PR with a clean, rebased branch containing your changes. Also please make sure tests are passing and update tests if necessary.
  To run the project locally, you will need to    npm installdev dependencies and then use    npm run devand    npm testto transpile ES2015 code and run tests, respectively (Node.js >= 6.0 or latest). See    package.jsonfor more info.  
  Contributors

  
       
  •       @svileng  
  License

  
       
  • docrel:LICENSE   
  • "Heresy" name and logo: copyright © 2016 Heresy Software Ltd  
邓思源 发表于 2016-10-12 00:48:52
请你吃包辣条
回复 支持 反对

使用道具 举报

renliuxin 发表于 2016-11-10 14:22:13
做为一只禽兽,我深感压力很大…
回复 支持 反对

使用道具 举报

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表