我的代码是这样的。
setCol (param) {
// missing forEach on NodeList for IE11
if (window.NodeList && !NodeList.prototype.forEach) {
NodeList.prototype.forEach = Array.prototype.forEach;
}
const allDates = document.querySelectorAll('.v-date-picker-table .v-btn .v-btn__content')
const dates = this.availableDates.map(x => parseInt(x.split('-')[2]))
allDates.forEach((x, i) => {
console.log(x.innerHTML) // example : 1, 2, 3, 4, 5 ,6 etc
console.log(parseInt(x.innerHTML)) // in firefox, chrome, it works. it return value. example : 1. But in ie 11, it return NaN
if (dates.indexOf(parseInt(x.innerHTML)) !== -1) {
console.log('a')
} else {
console.log('b')
}
})
},
这个问题只发生在浏览器中,即,我寻找引用,并尝试像这样。
parseInt(x.innerHTML, 10)
和
Number(x.innerHTML)
和
x.innerHTML * 1
但它是一样的,该 console.log(parseInt(x.innerHTML))
返回 NaN
如何解决这个问题?
JS Number Prototype应该可以帮你处理这个问题。我在IE11开发控制台中尝试了以下的方法。
var numberString = "1234";
var toNumber = Number(numberString);
console.log(typof(toNumber));
// console.log result: "number"
所以你的例子应该可以用 但是我不知道你是否在你的代码中同时做了太多的事情。我已经做了一个例子来说明我是如何将这个参数化的:) 让我知道它是否有效。
allDates.forEach((x, i) => {
var innerString = x.innerHtml
var numberFromInner = Number(innerString);
if (dates.indexOf(numberFromInner) !== -1) {
console.log('a')
} else {
console.log('b')
}
})