[英]Adding rows to multiple tables dynamically on one web page using Javascript
[英]Dynamically add rows in multiple tables in same page using one function
我有多個表,並且有一個向這些表添加行的函數:
<table id="table1" style=" border-collapse: collapse;">
<tr id="table1row1">...
<tr id="table1row2">...
<table id="table2" style=" border-collapse: collapse;">
<tr id="table2row1">...
<tr id="table2row2">...
問題是,當我添加行而不是上面的結果時,我將得到以下結果:
<table id="table1" style=" border-collapse: collapse;">
<tr id="table1row1">...
<tr id="table1row2">...
<table id="table2" style=" border-collapse: collapse;">
<tr id="table2row1">...
<tr id="table2row3">...
當我調用該函數向另一張表中添加一行時,它只是增加值
var thischannel=1;
var thisassignment=1;
var thisrow=1;
function addRow(list,table){
thisrow++;
if(something to place in this to point if this is a new table or the same table){
var thisrow=1;
}
}
無需將行數保留在變量中,只需找出表中有多少行並使用該數字來創建ID
var numRows = $(tableID).find("tr").size();
var id = tableID + "row" + (numRows + 1);
完整范例: http : //jsfiddle.net/HK6bA/
我將對所有表應用相同的類名,並在jQuery中以這種方式引用它們:
$(".className").append($("<tr><td>Hello World!</td></tr>"));
如果要使用ID,可以通過以下方式進行操作:
$("#table1, #table2").append("<tr><td>Hello World!</td></tr>"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.