为什么 Safari/Opera 不能使用此 JavaScript 代码?
我正在制作一个可以显示预订的日历。包含预订的高度是根据预订的长度按比例动态计算的。以下代码在 Firefox 中运行良好,但在 Safari 或 Opera 中运行不佳:
function calculateBookingHeight(from, to) {
var today = new Date;
var end = new Date.UTC(today.getUTCFullYear(),today.getUTCMonth(),today.getUTCDate(),23);
var start = new Date.UTC(today.getUTCFullYear(),today.getUTCMonth(),today.getUTCDate(),6);
var from = new Date(from);
var to = new Date(to);
if (from > start && to < end) {
var difference = (to - from) / 120000;
} else if (from > start && to > end) {
var difference = (end - from) / 120000;
} else {
var difference = 510
}
return difference;
}
总之,日历上每个小时的高度为 30px。第二个 if 语句处理预订在第二天结束的情况。
如果我用 return 510
替换整个代码块,Safari 会按预期运行,并将每个预订的高度设置为 510px,因此我认为一定是此函数中的某些内容导致了问题。
任何帮助将不胜感激。
谢谢罗宾
I am working on a calendar that will show bookings. The height of containing the booking is calculated dynamically in proportion to the length of the booking. The following code works perfectly well in Firefox but not in Safari or Opera:
function calculateBookingHeight(from, to) {
var today = new Date;
var end = new Date.UTC(today.getUTCFullYear(),today.getUTCMonth(),today.getUTCDate(),23);
var start = new Date.UTC(today.getUTCFullYear(),today.getUTCMonth(),today.getUTCDate(),6);
var from = new Date(from);
var to = new Date(to);
if (from > start && to < end) {
var difference = (to - from) / 120000;
} else if (from > start && to > end) {
var difference = (end - from) / 120000;
} else {
var difference = 510
}
return difference;
}
In summary, each hour on the calendar is a height of 30px. The second if statement deals with the end of the booking being the following day.
If I replace the entire code block with return 510
, Safari behaves as expected and sets the height of each booking to 510px so I assume it must be something in this function that is causing the problem.
Any help would be appreciated.
Thanks
Robin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的问题之一应该是
换句话说
,
UTC
是一种 getter 方法,这就是你获取对象的原因。要使用这些参数构造日期,只需将它们传递给构造函数即可。使用起来
会更直接。
One of your problems is that
should be
In other words,
UTC
is a getter method, which is why you were getting an object. To construct a date with those arguments, just pass them to the constructor.Using
would be more straightforward.
http://www.w3schools.com/js/js_obj_date.asp
描述创建具有四个构造函数的 javascript 日期对象:
正如 Luis 指出的那样,没有括号的调用不是有效的 js。
http://www.w3schools.com/js/js_obj_date.asp
Describes creation of a javascript date object with four constructors:
Your invocation without parens isn't valid js, as Luis points out.
想通了。未根据传递到函数的变量正确创建日期。我需要在创建日期之前单独解析输入。
Figured it out. The dates weren't being created properly from the variables passed into the function. I needed to separately parse the input before creating the dates.