[英]Script not running on Internet Explorer
My script is working fine on everything except internet explorer.我的脚本在除 Internet Explorer 之外的所有内容上都运行良好。 I'm not sure if it because I am using unsupported syntax or functions or what but if someone could take a look and see if anything stands out to them it would be really appreciated.
我不确定是因为我使用了不受支持的语法或函数还是什么,但如果有人可以看看是否有什么对他们来说很突出,那将不胜感激。
The basic implementation of the script is to create a simple shopping cart list when users click on a button.该脚本的基本实现是在用户单击按钮时创建一个简单的购物车列表。 The total increments to include the added item and the item name, quantity and price is listed.
列出了包含添加项目的总增量以及项目名称、数量和价格。
I have other scripts that work on my page so I know it is possible to implement onclick for example but I am unsure how to proceed here.我有其他脚本可以在我的页面上运行,所以我知道可以实现 onclick 例如,但我不确定如何在这里进行。
<div class="product-div" id="big-blue-barrel-div">
<h3>Big Blue barrel- $199</h3>
<div class="img-div">
<img src="images/barrel-pic.png" alt="picture of barrel" class="product-img">
</div>
<span>Quantity: </span>
<input class="qty bigBlueBarrelQty" type="number" name="qty" value="1" min="1" id="bigBlueBarrelQty">
<button type="button" onclick="addData(this.id, 'bigBlueBarrelQty')" name="button" id="bigBlueBarrel">Add to Cart</button>
</div>
var values = {
bigBlueBarrel: {
name: "Big Blue Barrel",
price: 199
},
bigBlueBox: {
name: "Big Blue Box",
price: 399
},
babyBlueBarrel: {
name: "Baby Blue Barrel",
price: 99
}
}
var total = 0;
var rowCount = 1;
function addData(clicked_id, quantity) {
var price = values[clicked_id].price;
var qty = document.getElementById(quantity).value;
var lineTotal = price * qty;
total += lineTotal;
document.getElementById("total").innerHTML = total;
addRow(clicked_id, qty, lineTotal);
if (qty == 1) {
alert(`${qty} ${values[clicked_id].name} has been added to your cart!
Scroll down to view your cart.`)
} else if (qty > 1) {
alert(`${qty} ${values[clicked_id].name}s have been added to your cart!
Scroll down to view your cart`);
}
}
function addRow(clicked_id, qty, lineTotal) {
var rows = "";
if (rowCount % 2 != 0 ) {
rows += `<tr class="odd"><td>${values[clicked_id].name}</td><td class="row-qty">${qty}</td><td>$${lineTotal}</td><td class="delete"><button onclick="deleteRow(this)" class="delete-btn">delete</button></td></tr>`;
$(rows).appendTo("#cart tbody");
rowCount++;
} else {
rows += `<tr class="even"><td>${values[clicked_id].name}</td><td class="row-qty">${qty}</td><td>$${lineTotal}</td><td class="delete"><button onclick="deleteRow(this)" class="delete-btn">delete</button></td></tr>`;
$(rows).appendTo("#cart tbody");
rowCount++;
}
}
function deleteRow(r) {
var i = r.parentNode.parentNode.rowIndex;
document.getElementById("cart").deleteRow(i);
}
Any thoughts would be appreciated.任何想法将不胜感激。
A lot of features in ES6 will not work in IE. ES6 中的许多功能在 IE 中无法使用。
Template literals
for example: ${qty} ${values[clicked_id].name}
Template literals
例如: ${qty} ${values[clicked_id].name}
In this case you need to rewrite your template literals
into normal strings using quotes:在这种情况下,您需要使用引号将
template literals
重写为普通字符串:
alert(qty + " " + values[clicked_id].name + " has been added to your cart!"+
"\n\n" +
"Scroll down to view your cart.");
This website:这个网站:
https://caniuse.com/#search=template%20literals https://caniuse.com/#search=template%20literals
and MDN are valuable resources when I comes to compatibility.当我谈到兼容性时,MDN 是宝贵的资源。
Rewrite:改写:
Tested on IE and it works!在 IE 上测试,它的工作原理!
var values = { bigBlueBarrel: { name: "Big Blue Barrel", price: 199 }, bigBlueBox: { name: "Big Blue Box", price: 399 }, babyBlueBarrel: { name: "Baby Blue Barrel", price: 99 } } var total = 0; var rowCount = 1; function addData(clicked_id, quantity) { var price = values[clicked_id].price; var qty = document.getElementById(quantity).value; var lineTotal = price * qty; total += lineTotal; document.getElementById("total").innerHTML = total; addRow(clicked_id, qty, lineTotal); if (qty == 1) { /*alert(`${qty} ${values[clicked_id].name} has been added to your cart! Scroll down to view your cart.`)*/ alert(qty + " " + values[clicked_id].name + " has been added to your cart!" + "\\n\\n" + "Scroll down to view your cart."); } else if (qty > 1) { /*alert(`${qty} ${values[clicked_id].name}s have been added to your cart! Scroll down to view your cart`);*/ alert(qty + " " + values[clicked_id].name + "s has been added to your cart!" + "\\n\\n" + "Scroll down to view your cart."); } } function addRow(clicked_id, qty, lineTotal) { var rows = ""; //optimizing this: /*rows += `<tr class="odd"><td>${values[clicked_id].name}</td><td class="row-qty">${qty}</td><td>$${lineTotal}</td><td class="delete"><button onclick="deleteRow(this)" class="delete-btn">delete</button></td></tr>`;*/ rows += '<tr class="' + (rowCount % 2 ? 'even' : 'odd') + '"><td>' + values[clicked_id].name + '</td><td class="row-qty">' + qty + '</td><td>' + lineTotal + '</td><td class="delete"><button onclick="deleteRow(this)" class="delete-btn">delete</button></td></tr>'; $(rows).appendTo("#cart tbody"); rowCount++; } function deleteRow(r) { var i = r.parentNode.parentNode.rowIndex; document.getElementById("cart").deleteRow(i); }
table#cart { margin: 40px; border: 1px solid #e3e3e3; border-collapse: collapse; } table#cart td { padding: 10px; border-right: 1px dashed #f2f2f2; } table#cart td > button{ padding: 10px; border: 1px solid #f2f2f2; background-color: #ff4444; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="product-div" id="big-blue-barrel-div"> <h3>Big Blue barrel- $199</h3> <div class="img-div"> <img src="images/barrel-pic.png" alt="picture of barrel" class="product-img"> </div> <span>Quantity: </span> <input class="qty bigBlueBarrelQty" type="number" name="qty" value="1" min="1" id="bigBlueBarrelQty"> <button type="button" onclick="addData(this.id, 'bigBlueBarrelQty')" name="button" id="bigBlueBarrel">Add to Cart</button> </div> <!-- added for testing --> <table id="cart"> <tbody> </tbody> </table> Total: <span id="total"></span>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.