【问题标题】:Loading text from a txt file depending on day of week根据星期几从 txt 文件加载文本
【发布时间】:2017-06-24 11:15:29
【问题描述】:

我正在使用它根据星期几加载图像,但我也想显示文本并从文件中加载它。我喜欢把它当作今天的菜单,所以厨师上传文本文件,比如monday.txt tuesday.txt,不需要乱码。

<html>

<head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<META HTTP-EQUIV="refresh" CONTENT="60">
<title>LOUNAS</title>
<style type="text/css">
body {
    overflow:hidden;
}
</style>
<script type="text/javascript"><!--
var imlocation = "";
 function ImageArray (n) {
   this.length = n;
   for (var i =1; i <= n; i++) {
     this[i] = ' '
   }
 }
image = new ImageArray(7);
image[0] = 'sunday.jpg';
image[1] = 'monday.jpg';
image[2] = 'tuesday.jpg';
image[3] = 'wednsday.jpg';
image[4] = 'thursday.jpg';
image[5] = 'friday.jpg';
image[6] = 'saturday.jpg';
var currentdate = new Date();
var imagenumber = currentdate.getDay();
document.write('<img src="' + imlocation + image[imagenumber] + '"> style="width:100%;height:100%;" border="0" /');
//--></script></head>

<body bgcolor="#000000">
</body>

</html>

【问题讨论】:

    标签: javascript jquery html css image


    【解决方案1】:

    您需要使用 AJAX 请求才能从服务器读取文件。 (如果您的目的是将数据显示给客户而不是自显示站点)

    您需要服务器和后端语言,如 PHP,然后您需要根据星期几(即哪个 url?)向服务器发出 AJAX 请求,然后显示数据。

    第二,关于你做了什么,使用document.write这不是最好的方法,因为它在使用后清除页面并且你拥有的所有数据都会消失(意思是HTML),并且以这种方式创建元素也不起作用在所有浏览器中都很好,您应该改用createElement

    此外,您可以使用 CSS 为该元素赋予样式,而不是通过 JavaScript 来实现

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      相关资源
      最近更新 更多