我正在动态生成元标记。默认情况下,我能够附加属于metatag原型的对象的属性。 namecontentID。但是,当我尝试向对象添加其他内容时,说foo不会添加。

如何添加自己的属性,以便可以像在示例中添加name idcontent一样添加到属性。

var headID = document.getElementsByTagName("head")[0];
var metaNode = document.createElement('meta');
metaNode.name = "name"; //appends
metaNode.id = "id"; //appends
metaNode.content = "content"; //appends
metaNode.foo = "bar"; //doesn't append
headID.appendChild(metaNode);


结果:<meta id="id" name="name" content="content">

想要:<meta id="id" name="name" content="content" foo="bar">

最佳答案

metaNode.setAttribute('foo','bar');

关于javascript - 修改元标记对象的原型(prototype),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7682598/

10-09 17:48
查看更多