【发布时间】:2020-08-11 00:50:10
【问题描述】:
我建立了一个网站,它使用 jQuery 根据狗的出生日期计算狗的当前年龄。 jQuery 可以在 google chrome 中运行,但不能在 safari 中运行,我一直无法弄清楚原因。
function puppyAge(){
let d = new Date()
let dob = $(`input[name=puppy-age]`).val()
dob = new Date(dob)
let age = Math.floor((d-dob)/ (7 * 24 * 60 * 60 * 1000))
if (age == 1) {
$(`input[name=puppy-age]`).after(`<p>${age} week</p>`)
}
else {
$(`input[name=puppy-age]`).after(`<p>${age} weeks</p>`)
}
}
$(document).ready(function(){
galleryNavigarion(); // scroll page down to each puppy
parentAge(); // calculate how old (in years) from DOB
puppyAge(); // calculate how old (in Months) from DOB
})
在 safari 上输出给我“NaN 周”,但在 chrome 上它给了我正确的数字。
【问题讨论】:
-
很可能是由于
dob字符串中的日期格式。你在里面输入的值是多少? -
不清楚您使用哪个字符串输入日期。但是尝试在使用前解析您的日期字符串:
const inputValue = $('input[name=puppy-age]').val()然后const dob = Date.parse(inputValue)