object.setAttribute()和object.style有什么区别?

What's the difference between object.setAttribute() and object.style?

本文关键字:object 什么 区别 setAttribute style      更新时间:2023-09-26

它们基本上做同样的事情,为什么需要两者呢?或者setAttribute是用于风格以外的东西?

object.setAttribute("style","background-color:#FFFFFF");

等于

object.style.backgroundColor = "#FFFFFF";

哪个更好或更方便使用?

setAttribute

可用于设置对象的任何属性,包括但不限于style。

style.<style> = value

用于设置或修改样式。

在你给的例子中,它们是相同的,但setAttribute适用于所有浏览器,但只有新版本的IE(9+)。对象。样式更兼容跨浏览器