2017-03-29 3 views
-3

JSのDOMテーブルのヘルプをお願いします。私がボタンをクリックすると、テーブルの例が自動的に作成されます。私はテーブルのタグ、thead、ボーダーとアラインメントセンター、キャプション、データ(行とセルのテキスト)、tfoot、行の削除、行の追加を作成するためのいくつかの関数を記述したい。Javascriptでテーブルを動的に作成する

私はそれを行う方法がわかりません。

+0

は、あなたがこれまでにしようとしているものを私たちが知っていることはできますか?いくつかのコード例がうまくいくでしょう。 – JasperZelf

+0

私は月の病気の後に学校のレッスンに座っていますが、私はこれを行う方法を理解していません:/私は何も書いていません:( – sebkowy

+0

まあ...スタックオーバーフローはあなたのためにあなたの宿題をするためにここにあなたを助けるためにここにあります;) jqueryを使用できますか?それとも普通のjavascriptでなければなりませんか? – JasperZelf

答えて

0

ここでは始めるための例を挙げますが、残りの機能は自分で追加する必要があります。

// get the first button by it's ID, and listen to the click event 
 
// add an empty table to the container div 
 
document.getElementById("btnTable").addEventListener("click", function(){ 
 
\t var basicTable = '<table id="table"></table>'; 
 
    document.getElementById('container').innerHTML = basicTable; 
 
}); 
 

 
// add contents to the table, ADD instead of REPLACE by using the += operator 
 
document.getElementById("btnTableContentsAdd").addEventListener("click", function(){ 
 
\t var tableContents = '<tr><td>Hello world!</td></tr>'; 
 
    document.getElementById('table').innerHTML += tableContents; 
 
}); 
 

 
// remove last table row 
 
document.getElementById("btnTableContentsRemove").addEventListener("click", function(){ 
 
    var lastRow = document.getElementById('table').lastElementChild; 
 
    document.getElementById('table').removeChild(lastRow); 
 
}); 
 

 
// set the border to 1px solid blue 
 
document.getElementById("btnTableBlueBorder").addEventListener("click", function(){ 
 
\t document.getElementById('table').style.border = "1px solid blue"; 
 
}); 
 

 
// set the border to 1px solid red 
 
document.getElementById("btnTableRedBorder").addEventListener("click", function(){ 
 
\t document.getElementById('table').style.border = "1px solid red"; 
 
});
<button id="btnTable">add empty table (invisible)</button> 
 
<button id="btnTableContentsAdd">add table contents</button> 
 
<button id="btnTableContentsRemove">remove table contents</button> 
 
<button id="btnTableBlueBorder">add blue table border</button> 
 
<button id="btnTableRedBorder">add red table border</button> 
 
<div id="container"></div>

+0

ありがとう:3私はそれを得たと思う;) – sebkowy

関連する問題