何时在JavaScript中使用setAttribute vs.Attribute=?

何时在JavaScript中使用setAttribute vs.Attribute=?

有一个关于使用setAttribute而不是点(.)制定了属性表示法?

例如:

myObj.setAttribute("className", "nameOfClass");myObj.setAttribute("id", "someID");

myObj.className = "nameOfClass";myObj.id = "someID";


慕妹3146593
浏览 282回答 3
3回答

ibeautiful

从…JavaScript:权威指南它澄清了一些事情。它注意到HTMLElementHTMLdoc的对象定义与所有标准HTML属性相对应的JS属性。所以你只需要用setAttribute对于非标准属性。例子:node.className = 'test'; // worksnode.frameborder = '0'; // doesn't work - non standard attributenode.setAttribute('frameborder', '0'); // works
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript