技术控

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

[其他] docrel — Slightly better document.createElement

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

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
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  
友荐云推荐




上一篇:Usersnap For Your Enterprise: 5 Noteworthy Features!
下一篇:解决共享卷和跨主机数据访问的难题:使用Convoy-NFS构建共享卷 ...
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

邓思源 发表于 2016-10-12 00:48:52
请你吃包辣条
回复 支持 反对

使用道具 举报

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

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表