gpt4 book ai didi

javascript - 当我尝试在我的页面上创建表格时出现此错误 - IndexSizeError : Index or size is negative or greater than the allowed amount

转载 作者:行者123 更新时间:2023-11-28 04:46:20 24 4
gpt4 key购买 nike

<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8">

<!-- ** JavaScript ** -->
<script src="products.js" charset="utf-8"></script>
<script src="purchased.js" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">

clear_table = function( ) {
var table = document.getElementById( "cart_table" );
while(table.hasChildNodes( )) {
table.removeChild( table.firstChild );
}
}

make_table = function( items ) {

// ** Load items into table and calc total **
var total = 0;

var table = document.getElementById("cart_table");
for(var x = 0; x < items.length; x++){
var tr = table.insertRow( x + 1 );
var item = tr.insertCell( 0 );
var price = tr.insertCell( 1 );
item.innerHTML = items[x].name;
price.innerHTML = "$" + items[x].price;

//remove button
var remove_btn = document.createElement( "button" );
remove_btn.className = "btn_remove";
remove_btn.href = "#";
remove_btn.index = x;
var btn_text = document.createTextNode( "x" );
remove_btn.appendChild( btn_text );

var remove = tr.insertCell( 2 );
remove.appendChild( remove_btn );

remove_btn.onclick = function() {
items.delete(this.index);

var ids = new Array();
for( var x = 0; x < items.length; x++ ){
ids.push( items[x].id );
}

save_purchased( ids );
clear_table( );
make_table( items );
return false;
}

//total
total += ( items[x].price );

}//end build table loop
//insert total
var tr = table.insertRow(x+1);
var item = tr.insertCell( 0 );
var price = tr.insertCell( 1 );
item.innerHTML = "Total: ";
price.innerHTML = "$" + total;

}

window.onload = function() {

// ** Get orders id form cookie **
var order = load_purchased();

// ** Load orders products from cookie id into items[] **
var items = new Array();
for( var x = 0; x < order.length; x++ ){
items.push( get_product( order[x] ) );
//alert( items[x].name );
}

make_table( items );

}//end window.onload()

</script>
</head>
<body>
<heading>

<h1>Cart</h1>

<a href="home.html"><button class="btn_cart">&larr; Home</button></a>

</heading>

<div id="content">

<table id="cart_table">
<tr>
<th>Item</th>
<th>Price</th>
<th>Remove</th>
</tr>
</table>

<a href="checkout.html"><button class="btn_cart">Order &rarr;</button></a>

</div>
</body>

是什么导致了这个错误? (IndexSizeError:索引或大小为负数或大于允许的数量)

错误发生在 make_table()remove_link.onclick 事件执行时。

这个 make_table() 函数首先从 window.onload() 执行,它创建了我的表。它执行良好,没有错误。

但是当我点击某个元素旁边的删除按钮将其从表格中删除时。 remove_link.onclick() 执行,它再次执行 make_table() 函数以重新创建没有不需要的元素的表并计算新的总数。

嗯...函数在第二次执行时不起作用,我得到这个错误(IndexSizeError: Index or size is negative or greater than the allowed amount)

控制台显示它在第 27 行,也就是这一行 (var tr = table.insertRow(x + 1);)。

但是,我可以刷新页面,页面将再次正常加载,我删除的元素消失了。所以只有当 make_table() 函数从 onclick 事件中被调用时。

最佳答案

我遇到了类似的情况。一遍又一遍地阅读我的代码后,我看到了愚蠢的错误。

let Row = LicenseTableBody.insertRow( rowIndex );
let cell0 = Row.insertCell(0);
// let cell1 = Row.insertCell(1);
let cell2 = Row.insertCell(2);
let cell3 = Row.insertCell(3);

它给了我标题中提到的错误。我想我错误地试图在 0 之后立即在 2 位置插入单元格。这不是一个好的顺序。

所以,我只是将顺序更改为正确的顺序,如下所示:

let Row = LicenseTableBody.insertRow( rowIndex );
let cell0 = Row.insertCell(0);
// let cell1 = Row.insertCell(1);
let cell2 = Row.insertCell(1);
let cell3 = Row.insertCell(2);.

当然,我犯了一个愚蠢的错误。

希望对您有所帮助,

关于javascript - 当我尝试在我的页面上创建表格时出现此错误 - IndexSizeError : Index or size is negative or greater than the allowed amount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40940293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com