基于proxy封装的对于dom元素的attribute的读写操作小工具库
基于 proxy 封装的对于 dom 元素的 attribute 的读写操作库
yarn add https://github.com/masx200/dom-element-attribute-agent-proxy.git
或者
cnpm install --save https://github.com/masx200/dom-element-attribute-agent-proxy.git
declare function createeleattr(ele: HTMLElement): Record<string, any>;
import createeleattr from "@masx200/dom-element-attribute-agent-proxy";
var element1 = document.querySelector(
"div.Popover.anim-scale-in.js-tagsearch-popover"
);
var attribute1 = createeleattr(element1);
attribute1.testarray = [1, "sssssssq"];
console.log(attribute1.testarray);
console.log(Reflect.ownKeys(attribute1));
console.log(attribute1.class);
attribute1["data-tagsearch-ref"] = "master";
console.log(Object.entries(attribute1));
attribute1.class = new Set(["wwwwwwwww1", "sssssssq"]);
attribute1.style = { display: "block", width: "500px" };
console.log(attribute1.style);
attribute1.style = "display:block;width:500px;height:100px";
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
的读写操作,input
和 textarea
和select
元素 的 value
的读写操作,style
属性的对象类型和字符串类型style
属性支持驼峰命名法和横杠命名法,自动转换成字符串class
属性对于 Array
和 Set
的支持,也增加了其他属性对于 Set
转 Array
的支持Proxy
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Proxy