javascript - 无法在我的 HTML/JS 页面上获取背景图片

标签 javascript image css background

这是代码,我无法获得 body 上的蛋糕图像或整个 html。我试过像 body 背景这样的 HTML,我试过 style ,我试过 js,没有任何效果。有趣的是,我已经在其他页面上测试了图像,加载正常。我也尝试过使用 html、css、js 修改背景颜色,它们都可以正常工作。但是该死的图像不会加载。有什么想法吗?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Age Calculator</title>
</head>
<body>


  <style>
    #myDiv {
      background-image: url('images/cake.png');
      background-size: cover;
    }

    </style>

<div id="myDiv">
<script>

//document.body.style.backgroundImage = "url('images/cake.jpg')";
//document.getElementById("myDiv").style.backgroundImage =     "url('images/cake.jpg')";  


let birthYear = prompt("Enter Your Birth Year");
let birthMonth = prompt("Enter Your Birth Month (As A Number)");
let birthDay = prompt("Enter Your Birth Day");
let date = new Date();
let currentYear = date.getFullYear();
let currentMonth = date.getMonth() + 1;
let currentDay = date.getDate();
let yearAge = currentYear - birthYear;
let monthAge = currentMonth - birthMonth;
let dayAge = currentDay - birthDay;
let monthPluralizer = "";
let dayPluralizer = "";

if (monthAge === 1) {
  monthPluralizer = "Month";
} else {
  monthPluralizer = "Months";
}

if (dayAge === 1) {
  dayPluralizer = "Day";
} else {
  dayPluralizer = "Days";
}

if (currentMonth < birthMonth) {
  monthAge = monthAge + 12;
  yearAge = yearAge - 1;
}

if (currentDay < birthDay) {
  monthAge = monthAge - 1;
  if (currentMonth === 1 || 3 || 5 || 7 || 8 || 10 || 12) {
    dayAge = dayAge + 31;
  } else if (currentMonth === 4 || 6 || 9 || 11) {
    dayAge = dayAge + 30;
  } else if (currentMonth === 2) {
    dayAge = dayAge + 28;
  }
}

if (currentMonth == birthMonth && currentDay < birthDay) {
  monthAge = monthAge + 12;
  yearAge = yearAge - 1;
}


document.write("<p>Your Birth Date Is " + birthMonth + "/" + birthDay + "/" + birthYear + "</p><br>");
document.write("<p>Today's Date Is " + currentMonth + "/" + currentDay + "/" + currentYear + "</p><br>");
document.write("<p>You Are " + yearAge + " Years, " + monthAge + " " + monthPluralizer + " & " + dayAge +
  " " + dayPluralizer + " Old.</p><br>");
if (birthMonth == 0 && birthDay == 0 && birthYear == 0 ) {
  document.write("<p>Hello Jesus!<p>")
} else if (birthMonth === null && birthDay === null && birthYear === null ) {
  document.write("<p>Hello Jesus!<p>")
}





</script>
</div>
</body>
</html>

最佳答案

你的图片路径有误

试试这个:

   #myDiv {
      background-image: url('./images/cake.png');
      background-size: cover;
    }

如果不行告诉我

您可以阅读有关 HTML Paths here 的更多信息并在这个问题中看到另一个像你这样的帖子 here

关于javascript - 无法在我的 HTML/JS 页面上获取背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52539529/

相关文章:

javascript - 在 React Native 中存储静态和公共(public)数据

javascript - 使用 JavaScript 更新 Css 类

html - &lt;input/> inside <label> 在 Firefox 中不工作

javascript - fabricjs 路径的边界框偏移量

javascript - 我们如何在 Fabric.js 中限制 Canvas 对象的最大宽度和高度

javascript - gif 加载后更改背景图像?

php - 如何使用给定的 X、Y、宽度和高度坐标在 php 中裁剪图像

python - OpenCV:形状检测

html - 使用方形元素符号将 ul 列表及其元素符号点居中

css - 使用 "pointer-events: none"时添加 CSS 光标属性