gpt4 book ai didi

javascript - 使用javascript更改背景

转载 作者:行者123 更新时间:2023-11-28 12:07:40 25 4
gpt4 key购买 nike

我必须使用 JavaScript 更改 div 的背景。我已经成功做到了这一点使用 document.getElementById('test').style.background = "url('img/test.jpg')";

现在,我如何更改其他属性,例如重复、滚动等?

我想要测试的CSS就像

background: #f00 url('img/test.jpg') no-repeat fixed 10px 10px;

我不能使用 jQuery,因为我不想只为了一件小事而包含该库。

最佳答案

而不是使用 javascript 设置所有 css 属性。我建议为这个具有特定类的元素创建一个额外的 css 规则。然后在需要时使用 javascript 在该元素中添加或删除此类。

例如。

function changeBG(){
var element = document.getElementById('test');
element.setAttribute("class", 'newBG'); //For Most Browsers
element.setAttribute("className", 'newBG'); //For IE; harmless to other browsers.
}

关于javascript - 使用javascript更改背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7497333/

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