我是JavaScript新手,我正在尝试创建一个网页,如果我每周有5天相同的开放和关闭时间,它会将它们输出到同一行。例如,星期一到星期五的小时数在我的示例中是相同的,因此它将打印出来:
周一至周五:上午6:00至下午2:00 周六上午8:00 - 下午1:00 星期日休息(没有连字符)
当我在浏览器中运行时,我得到一个空白页面。这是我的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<script>
var MondayOpen = "6:00AM - ";
var MondayClose = "2:00PM";
var TuesdayOpen = "6:00AM - ";
var TuesdayClose = "2:00PM";
var WednesdayOpen = "6:00AM - ";
var WednesdayClose = "2:00PM";
var ThursdayOpen = "6:00AM - ";
var ThursdayClose = "2:00PM";
var FridayOpen = "6:00AM - ";
var FridayClose = "2:00PM";
var SaturdayOpen = "8:00AM - ";
var SaturdayClose = "1:00PM";
var SundayOpen = "Closed";
var SundayClose = "";
var dateArray = new Array(MondayOpen, MondayClose, TuesdayOpen, TuesdayClose, WednesdayOpen, WednesdayClose, ThursdayOpen, ThursdayClose,FridayOpen, FridayClose, SaturdayOpen, SaturdayClose, SundayOpen, SundayClose);
function outputDate(){
for(int i = 0; i<dateArray.length; i++){
var current;
//Puts Open and Close time into 1 string
dateArray[i] + dateArray[i+1] = current;
//Compare the two strings
if(current.substring(0,15) == current.substring(0,15) +2)
}
$("#hours").html(<b> current <b/>);
}
var getHours = document.getElementById('hours').innterHTML = current;
}
</script>
<div id= "hours" style="font-size:10px; color:#fff;">
</div>
我在这里缺少什么?任何帮助将不胜感激。
答案 0 :(得分:1)
这里有几个错误。仅举几例:
您在此处放置int
而不是var
:
for(var i = 0; i<dateArray.length; i++){
您在此处放置String
而不是var
:
var curr;
您是要为表达式分配变量吗?你的意思是反过来这样做吗?
//Puts Open and Close time into 1 string
curr = dateArray[i] + dateArray[i+1];
你的if
声明......
//Compare the two strings
if(current.substring(0,15) == current.substring(0,15) +2)
{
您输入innterHTML
而不是innerHTML
并尝试执行多项分配:
var getHours = current;
document.getElementById('hours').innerHTML = current;
您忘记将其括在引号中:
$("#hours").html("<b> current <b/>");
即使修复了所有这些语法错误,我也会得到一个空白页面。考虑从头开始重写。
答案 1 :(得分:1)
在Javascript中,您不需要声明String theString =“This is a string”。 只需写var stringName =“这是我的字符串。”
答案 2 :(得分:0)
您可能想退后一步,掌握JavaScript和HTML。有许多免费资源可供使用。简单来说,为什么不试试Code Academy或W3 schools。话虽这么说,这足以修复你的变量。
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
</head>
<body>
<div id="hours"></div>
</body>
<script type="text/javascript">
var MondayOpen = "6:00AM - ";
var MondayClose = "2:00PM";
var TuesdayOpen = "6:00AM - ";
var TuesdayClose = "2:00PM";
var WednesdayOpen = "6:00AM - ";
var WednesdayClose = "2:00PM";
var ThursdayOpen = "6:00AM - ";
var ThursdayClose = "2:00PM";
var FridayOpen = "6:00AM - ";
var FridayClose = "2:00PM";
var SaturdayOpen = "8:00AM - ";
var SaturdayClose = "1:00PM";
var SundayOpen = "Closed";
var SundayClose = "";
var dateArray = new Array(MondayOpen, MondayClose, TuesdayOpen, TuesdayClose, WednesdayOpen, WednesdayClose, ThursdayOpen, ThursdayClose,FridayOpen, FridayClose, SaturdayOpen, SaturdayClose, SundayOpen, SundayClose);
function outputDate(){
var printOutResults;
for(i = 0; i<dateArray.length; i++) {
var current;
//Puts Open and Close time into 1 string
current = dateArray[i] + dateArray[i+1];
printOutResults += current;
//Compare the two
if(current.substring(0,15) == current.substring(0,15) +2){
$("#hours").html(current);
}
}
$("#hours").html(printOutResults);
}
outputDate();
</script>