<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 8 年前。
标签 javascript css
<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 8 年前。
document.body.style.backgroundImage
标签有问题。
它在本地完美运行,但在服务器上运行不正常。
<a href="roztoky.php" onclick = "document.body.style.backgroundImage = 'url(../img/roztoky.png)';">
在本地,背景切换没问题,但在服务器上,背景只出现了几秒钟,然后就被覆盖回默认
但如果我这样调用它:
<a href="_section/roztoky.php" onclick = "document.getElementById('frame').style.backgroundImage = 'url(img/roztoky.png)';" target="a">
有效...
有什么想法吗?
最佳答案
使用jquery
改变
<a id="a" href="roztoky.php" onclick = "document.body.style.backgroundImage = 'url(../img/roztoky.png)';">
顶
$('#a').on('click', function() {
$('body').css('background-image', 'url(/img/roztoky.png)');
});
和
<a id="b" href="_section/roztoky.php" onclick = "document.getElementById('frame').style.backgroundImage = 'url(img/roztoky.png)';" target="a">
到
$('#b').on('click', function() {
$('#frame').css('background-image', 'url(img/roztoky.png)');
});
或者使用纯 javascript
var button_a = document.querySelector("#a"),
button_b = document.querySelector("#b"),
myframe = document.querySelector("#frame");
button_a.addEventListener("click",function(e){
document.body.style["background-image"] = "url(https://c2.staticflickr.com/6/5530/11442019345_d50b753156.jpg)";
},false);
button_b.addEventListener("click",function(e){
myframe.style.style["background-image"] = "url(https://c2.staticflickr.com/4/3702/11442187163_fdd370b657_n.jpg)";
},false);
HTML::
<a id="a">body background image</a>
<a id="b">change frame background</a>
<div id="frame"></div>
CSS::
#frame{
width:100%;
height:320px;
}
a{
padding: 6px 12px;
background:#ccc;
border-radius:6px;
cursor:pointer;
color:white;
}
关于javascript - document.body.style.backgroundImage 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25771401/
相关文章:
javascript - 如何判断一个javascript变量的数据类型是Array?
javascript - 如何使用 @solana/web3.js 从 Solana 中的自定义 token 中删除类型转换权限?
javascript - 在 Angular2 中的 router.navigate 之后执行代码
javascript - 如何使用 three.js/webgl 将 2d 图转换为 3drawing
animation - 如何使用 css3 动画更改背景位置?
javascript - 如何使用js查询Polymer中的shadow dom元素?