本文介绍了document.setAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可以用来改变CSS吗?不能看到很多关于它的w3。任何人都知道。

Can this be used to change CSS? Cant see much on w3 about it. Anyone know anything about it.

如果这个论坛/网站不是问问浏览器标准问题的地方,有人可能指向我正确的方向吗?

If this forum/site isnt the place for asking browser standard questions, can someone point me in the right direction?

感谢

- Mark

推荐答案

是的,您可以使用setAttribute更改单个DOM元素的CSS,如下所示:

Yes, you can use setAttribute to change the CSS of a single DOM element, like so:

document.getElementById("something").setAttribute("style", "color: red;");

但是,我认为这是不好的做法。您可以按照Marius指出的方式修改样式表:

However, I believe it's bad practice. You can modify the stylesheet as Marius pointed out by doing:

document.styleSheets[0].cssRules[0].backgroundColor = "#FF0000";

或通过手动编辑HTML元素的样式属性,如:

or by manually editing the style attributes of HTML elements, like:

document.body.style.backgroundColor = "#FF0000";

我建议您查看,因为它具有功能强大,易于使用的工具来修改DOM元素的CSS。它很简单:

I would recommend looking into jQuery, as it has powerful and easy to use tools for modifying the CSS of DOM elements. It's as simple as:

$("#someID").css({"color": "red", "width": "100px"});

这篇关于document.setAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 03:21