你可能应该做这样的事情:
var el = document.getElementById("myNode");
el.setAttribute('updated', true);
el.setAttribute('foo', 'bar');
el.textContent = 'new text';
编辑:
为了使其更具动态性,您可以编写如下所示的函数来遍历属性对象及其值并将它们应用于目标元素。
使用 vanilla javascript 似乎您希望实现的目标非常困难,我会考虑使用某种库,可能会做出反应,因为您似乎希望根据状态进行更改。
// Function
function updateElement (element, attributes) {
for (attr in attributes) {
element.setAttribute(attr, attributes[attr]);
}
}
// Use
var myNode = document.getElementById("myNode");
var attributes = {
updated: true,
foo: bar
};
updateElement(myNode, attributes);