javascript - DIV 不显示
问题描述
我想知道为什么我的 DIV 不会出现。控制台中没有弹出错误,所以我不确定是什么问题。我还检查了 Google Chrome 扩展程序的内容安全政策,看看我的代码是否没有遵循该政策,但一切似乎都很好。
HTML:
<div id="date"></div>
JavaScript:
(function() {
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
Date.prototype.getMonthName = function() {
return months[ this.getMonth() ];
};
Date.prototype.getDayName = function() {
return days[ this.getDay() ];
};
})();
var now = new Date();
var day = now.getDayName();
var month = now.getMonthName();
CSS:
#date {
display: block;
color: black;
font-size: 50px;
top: 50px;
left: 50px;
}
解决方案
您需要填充日期 div 以使其显示某些内容。空 div 不占用任何空间。我使用 innerHTML 将日期和月份变量移动到元素中:
(function() {
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
Date.prototype.getMonthName = function() {
return months[this.getMonth()];
};
Date.prototype.getDayName = function() {
return days[this.getDay()];
};
})();
var now = new Date();
var day = now.getDayName();
var month = now.getMonthName();
document.getElementById("date").innerHTML = day + " " + month;
#date {
display: block;
color: black;
font-size: 50px;
top: 50px;
left: 50px;
}
<div id="date"></div>
推荐阅读
- c# - ReactJS 不会在更新过程中替换页面内容
- c++ - 如果在 C++ 中定义了类型,是否可以在预处理器时检测到?
- haskell - 如何在 Haskell 中创建格型数据结构?
- android - 有没有办法知道动态壁纸的 SettingsActivity 是如何启动的?
- sequential - 如何顺序运行lerna?
- python - `plt.imshow` 只生成 suplot 的最后一张图片
- python - Pandas 中这种 df.drop() 行为的可能原因是什么?
- node.js - 如何将midi文件转换为mp3文件?
- html - 如何将 CSS 放在背景图片上
- json - 通用字体字距映射