gpt4 book ai didi

javascript - 有没有一种方法不仅可以向 HTML 元素添加/删除一个类,而且还可以实际更改 CSS 类中的值?

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:35 24 4
gpt4 key购买 nike

例如:我想通过按下按钮来显示随机背景颜色。我很高兴如果我把字符串放在一起会产生一个通常的十六进制代码的变量......我只能如何将这些随机值写入CSS文件?有什么办法吗?

我不想更改类名,但想更改类名包含的内容作为背景颜色的样式值!

function changeColor() {
var content = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g"];
var one = content[Math.floor(Math.random()*content.length)];
var two = content[Math.floor(Math.random()*content.length)];
var three = content[Math.floor(Math.random()*content.length)];
var four = content[Math.floor(Math.random()*content.length)];
var five = content[Math.floor(Math.random()*content.length)];
var six = content[Math.floor(Math.random()*content.length)];

最佳答案

如果你想改变内联样式你可以看看this页:

Changing HTML Style - To change the style of an HTML element, use this syntax:

document.getElementById(id).style.property = new style The following example changes the style of a <p> element:

Example:

<html>
<body>

<p id="p2">Hello World!</p>

<script>
document.getElementById("p2").style.color = "blue";
</script>

<p>The paragraph above was changed by a script.</p>

</body>
</html>

 <!DOCTYPE html>
<html>
<body>

<h1 id="id1">My Heading 1</h1>

<button type="button"
onclick="document.getElementById('id1').style.color = 'red'">
Click Me!</button>

</body>
</html>

或者

 <!DOCTYPE html>
<html>
<body>

<h1 id="id1" style="color: blue">My Heading 1</h1>

<button type="button"
onclick="document.getElementById('id1').style = 'color: red'">
Click Me!</button>

</body>
</html>

关于javascript - 有没有一种方法不仅可以向 HTML 元素添加/删除一个类,而且还可以实际更改 CSS 类中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50457071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com