将DOM元素的属性/值对转换为Javascript对象?

pro*_*ype 3 javascript jquery

是否有一个快捷方式函数将DOM元素及其各种属性/值对转换为相应的Javascript对象?

例如,在HTML页面中转换它:

<div id="snack" type="apple" color="red" size="large" quantity=3></div>
Run Code Online (Sandbox Code Playgroud)

到Javascript中的对象,就好像你输入了:

var obj = {
        id:  "snack"
        type: "apple"
        color: "red"
        size: "large"
        quantity: 3
};
Run Code Online (Sandbox Code Playgroud)

Jul*_*May 6

不是真正的捷径,但至少是一个做你想要的简短实现:

var attributes = document.getElementById('someId').attributes;
var obj = {};

for (var i = 0, len = attributes.length; i < len; i++) {
    obj[attributes[i].name] = attributes[i].value;
}
Run Code Online (Sandbox Code Playgroud)