2014-09-10 195 views
1

我有一个document.body.style.backgroundImage标记问题。 它在本地完美地唤醒,但不在服务器上。document.body.style.backgroundImage does not work

<a href="roztoky.php" onclick = "document.body.style.backgroundImage = 'url(../img/roztoky.png)';"> 

在当地,backgound是没有问题交换,但在服务器上的研究背景卡梅斯起来只是几秒钟,然后它的overwrited回defalut

但如果我把它叫做这样的:

<a href="_section/roztoky.php" onclick = "document.getElementById('frame').style.backgroundImage = 'url(img/roztoky.png)';" target="a"> 

它的工作原理...

任何想法?

+1

您的图像路径各不相同。我会认为这是问题的一部分。 – 2014-09-10 17:23:12

+0

这是怎么回事你为什么使用内嵌脚本这是非常糟糕的 – 2014-09-10 17:25:45

+0

我们看不到你的在线页面*或*你的本地页面,那么你如何指望我们解决这个问题呢?这很可能是文件位置的一个问题,在问题中根本没有公开。 – 2014-09-10 17:26:54

回答

0

使用jQuery

DEMO:

变化

<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

DEMO

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; 
} 
+0

将尽快尝试并让你知道。感谢迄今 – DaPo 2014-09-10 17:39:56

+0

非常感谢。有效 – DaPo 2014-09-10 20:11:30