简体   繁体   English

JavaScript语法错误(我认为)

[英]JavaScript syntax error (I think)

I'm working on my homework for class and I copied this code straight out of the book and it's not working. 我正在上课的家庭作业,我直接从书中复制了此代码,但没有用。 The problem is the copyRight() function isn't displaying. 问题是没有显示copyRight()函数。 The countDown() function is being display somewhere else and it works perfectly. countDown()函数正在其他地方显示,并且效果很好。 Does anyone see what I'm doing wrong here? 有人在这里看到我在做什么错吗?

function countDown() {
    var today = new Date()
    var dayofweek = today.toLocaleString()
    dayLocate = dayofweek.indexOf(" ")
    weekDay = dayofweek.substring(0, dayLocate)
    newDay = dayofweek.substring(dayLocate)
    dateLocate = newDay.indexOf(",")
    monthDate = newDay.substring(0, dateLocate+1)
    yearLocate = dayofweek.indexOf("2013")
    year = dayofweek.substr(yearLocate, 4)

    var bridalExpo = new Date("February 12, 2014")
    var daysToGo = bridalExpo.getTime()-today.getTime()
    var daysToBridalExpo = Math.ceil(daysToGo/(1000*60*60*24))

    displayCountDown.innerHTML = "<p style='font-size:12pt; font-family: helvetica;'>Today is "
        +weekDay+" "+monthDate+" "+year+". We Have "+daysToBridalExpo+
        " days until the Midwest Bridal Expo.</p>"
}

function copyRight() {
    var lastModDate = document.lastModified
    var lastModDate = lastModDate.substring(0,10)
    displayCopyRight.innerHTML = "<p style='font-size:12pt; font-family:helvetica;'>Today is "
        +weekDay+" "+monthDate+" "+year+". We have "+daysToBridalExpo+
        " days until the Midwest Bridal Expo.</p>"
}

The body tag looks like this: (the other two functions work great) body标签看起来像这样:(其他两个功能很好用)

<body onload="scrollColor(); countDown(); copyRight()">

The call for the copyRight() function looks like this: copyRight()函数的调用如下所示:

<div id="displayCopyRight"></div>

I've been looking at this for the past half hour and am not seeing it. 在过去的半小时中,我一直在查看此内容,但没有看到它。 This is also the first time I've written javascript too though so I'm probably missing something. 这也是我也是第一次编写JavaScript,因此我可能会丢失一些东西。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

Thanks, Tommy 谢谢汤米

It's not a syntax error if something isn't being displayed. 如果未显示某些内容,则不是语法错误。 Syntax errors are when you write something which the parser for the language doesn't understand and It's displayed in your browser (firebug, developer tools, etc). 语法错误是指您编写了某种语言所无法理解的语法解析器,并将其显示在浏览器中(萤火虫,开发人员工具等)。

However, you have 2 functions 但是,您有2个功能

A

And

B

Variables that are defined in A as local variables cannot be accessed from function B. 无法从函数B访问在A中定义为局部变量的变量。

Local variables in A cannot be accessible from function B so you shouldn't be able to use 无法通过函数B访问A中的局部变量,因此您不应使用

daysToBridalExpo 

in copyRight because the variables don't exist in that scope. 在copyRight中,因为变量不在该范围内。

I recommend starting with something more simple. 我建议从更简单的内容开始。 Have you tried hello world to begin with and basic parametric returning? 您是否尝试过hello world并从基本参数返回? You could make function A return the needed variables to function B. 您可以使函数A将所需的变量返回给函数B。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM