简体   繁体   English

moment()函数可在html中工作,但不能在单独的javascript文件中工作

[英]moment() function works in html but not in separate javascript file

I have included moment.min.js in my page 我在页面中包含了moment.min.js

<script src="js/jquery.js"></script>
<script src="js/modernizr.js"></script>
<script src="js/moment.min.js"></script>
<script src="js/fatcalc.js"></script>

I can call 我可以打电话

<script>document.write(moment());</script>

and it displays the date fine on my page. 并在我的页面上显示正确的日期。

But, when I call it from within fatcalc.js 但是,当我从fatcalc.js中调用它时

var date = moment();

I get the error: 我得到错误:

'moment' is not defined. 未定义“时刻”。

Why can my html page see it, but not the fatcalc.js file? 为什么我的html页面可以看到它,但是fatcalc.js文件看不到?

Well, I figured it out. 好吧,我知道了。 Seems to be some issue with Jshint I don't understand but adding this to the top of my script fixed it. 似乎是Jshint的问题,我不了解,但是将其添加到脚本的顶部即可解决该问题。

/*global moment:true */

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

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