项目作者: masx200

项目描述 :
基于proxy封装的对于dom元素的attribute的读写操作小工具库
高级语言: TypeScript
项目地址: git://github.com/masx200/dom-element-attribute-agent-proxy.git
创建时间: 2019-09-11T15:59:50Z
项目社区:https://github.com/masx200/dom-element-attribute-agent-proxy

开源协议:GNU General Public License v3.0

下载


dom-element-attribute-agent-proxy

基于 proxy 封装的对于 dom 元素的 attribute 的读写操作库

安装方法

  1. yarn add https://github.com/masx200/dom-element-attribute-agent-proxy.git

或者

  1. cnpm install --save https://github.com/masx200/dom-element-attribute-agent-proxy.git

API

  1. declare function createeleattr(ele: HTMLElement): Record<string, any>;

使用方法

  1. import createeleattr from "@masx200/dom-element-attribute-agent-proxy";
  2. var element1 = document.querySelector(
  3. "div.Popover.anim-scale-in.js-tagsearch-popover"
  4. );
  5. var attribute1 = createeleattr(element1);
  6. attribute1.testarray = [1, "sssssssq"];
  7. console.log(attribute1.testarray);
  8. console.log(Reflect.ownKeys(attribute1));
  9. console.log(attribute1.class);
  10. attribute1["data-tagsearch-ref"] = "master";
  11. console.log(Object.entries(attribute1));
  12. attribute1.class = new Set(["wwwwwwwww1", "sssssssq"]);
  13. attribute1.style = { display: "block", width: "500px" };
  14. console.log(attribute1.style);
  15. attribute1.style = "display:block;width:500px;height:100px";
  16. console.log(attribute1.style);

设置attributes类型说明

style属性支持类型为string|Record<string,string>

class属性支持类型为string|Array<string>|Set<string>

其他属性支持Set<any>|Array<any>|string|Record<string,string>|Object

如果设置属性的值为类型Object,则将其用JSON.stringify转换成string类型

更新 : 添加了对于 input元素中 type="checkbox"checked 的读写操作,

更新 : 添加了对于 inputtextareaselect元素 的 value 的读写操作,

支持style 属性的对象类型和字符串类型

更新:style属性支持驼峰命名法和横杠命名法,自动转换成字符串

更新 :添加了 class 属性对于 ArraySet 的支持,也增加了其他属性对于 SetArray 的支持

关于 Proxy

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Proxy