javaScript:window.open不显示背景图像



请帮帮我,我已经为此工作了好几个小时了。我刚开始玩JavaScript。

所以我有一个按钮,加载一个新的浏览器窗口,加载背景图像。如下所示,没有问题。

HTML

<script>
setBackground();
</script>

JavaScript

function setBackground() {
document.body.style.backgroundColor = "#000000";
document.body.style.backgroundImage = "url(bg.jpg)"
}

然后我试着点击按钮加载另一个窗口,但背景图像无法工作。窗口工作,我可以更改背景颜色没有问题,但我无法加载图像。下面的代码。

//load screen on button click
document.getElementById("startButton").onclick = function() { 
  load()
  
};

function load() {
//create the new screen
newWindow = window.open("", "", "width=1550, height=800");
newWindow.id = "game";
newWindow.document.body.style.backgroundColor = "#000000";
newWindow.document.body.style.backgroundImage = "url(bg.jpg)";

}

文件和图像位于同一个文件夹中,我使用的是同一个图像,它在第一个示例中有效,但在第二个示例中无效。

请帮忙!!!提前谢谢。

检查此链接并单击Run

https://www.w3schools.com/code/tryit.asp?filename=GM9IO9Q6SDNL

试试这个。

<!DOCTYPE html>
<html>
<body onload="setBackground()" >
</body>
<script>
function setBackground() {
document.body.style.backgroundImage = "url(https://www.w3schools.com/html/pic_trulli.jpg)"
newWindowSetBackgroundload()
}
function newWindowSetBackgroundload() {
//create the new screen
var newWindow = window.open("", "", "width=1550, height=800");
//Id use for Uniq thats why its render only one time
//  newWindow.id = "game";
//use class for multiple windows genration 
newWindow.class = "game";
newWindow.document.body.style.backgroundColor = "#000000";
newWindow.document.body.style.backgroundImage = "url(https://www.w3schools.com/html/pic_trulli.jpg)";

}
</script>
</html>

最新更新