【发布时间】:2021-03-15 09:37:41
【问题描述】:
我用一些 js 代码制作了一个简单的 html 页面,我使用了所有 vanilla JS 并检查了是否使用了与 IE 兼容的所有功能。
问题是以下页面必须在谷歌浏览器中运行,而在 IE 中它不起作用,我在 IE11 和 IE9 中尝试过,但在两者中都不起作用。
代码只是将点击事件添加到三个按钮,按钮+ / - 改变输入和确认按钮内的数量。
js 看起来是这样的:
<script>
const btns = document.querySelectorAll('.btn-qty');
const confirms = document.querySelectorAll('.btn-success');
const inputs = document.querySelectorAll('.qty')
confirms.forEach(function(confirm) {
confirm.addEventListener('click', confirmHandler);
});
function confirmHandler(event) {
event.preventDefault();
let product_id = this.getAttribute("data-product-id");
let option_id = this.getAttribute("data-option-id");
let iva = parseInt(this.getAttribute("data-iva"));
let price = this.getAttribute("data-price");
let qty = this.getAttribute("data-qty");
window.location.href = "_?productId=" + product_id + "&optionId=" + option_id + "&iva=" + iva + "&price=" + price + "&qty=" + qty
}
inputs.forEach(function(input) {
input.addEventListener('change', function(event) {
event.preventDefault();
let btn = input.parentNode.querySelector(".input-group-prepend").querySelector('.btn-qty');
let confirm = input.parentNode.parentNode.querySelector('.btn-success')
let curVal = parseInt(input.value)
confirm.setAttribute('data-qty', curVal)
if (curVal >= 1) {
btn.removeAttribute('disabled');
}else if(curVal <= 1){
btn.setAttribute('disabled', true);
}
});
});
btns.forEach(function(btn) {
btn.addEventListener('click', function(event) {
event.preventDefault();
let input = btn.parentNode.parentNode.querySelector('.qty')
let curVal = parseInt(input.value)
let type = btn.getAttribute("data-type");
if (type == 'minus') {
if (curVal > 1) {
input.value = curVal - 1
input.dispatchEvent(new Event('change'));
}
if (parseInt(input.value) == 1) {
btn.setAttribute('disabled', true)
}
}else if (type == 'plus'){
input.value = curVal + 1
input.dispatchEvent(new Event('change'));
}
});
});
</script>
这是在 Chrome(顶部)和 IE11(底部)上运行的相同代码
这是JSfiddle的代码
【问题讨论】:
-
IE9/11 出现什么错误?
-
你的控制台可能有错误
-
@Berto99 控制台是空的..
-
@KasperJuner 控制台确实给我一个错误:i.stack.imgur.com/QyJGz.png 检查提供的链接 Berto99。您不能在
HTMLCollection(由querySelectorAll返回)上使用.forEach。我只会使用常规的 for 循环。
标签: javascript internet-explorer cross-browser