2016-10-28 10 views
0

JSとJqueryで行と列を追加してテーブルを作成します。 これは私のコードです:すべてのセルに対してJquery contenteditableが行でない

function AddColumnToDataTable(){ 
     $('#tableHeader').append("<th> Header </th>").attr("contenteditable", true); 
// Add a new ColumnHeader and set the property "editable" 
    } 

    function AddRowToDataTable(){ 
     var count = $('#tableHeader').find("th").length; 
// Get the count of Columns in the table 

     var newRow = $('#tableBody').append("<tr></tr>"); 
// Add a new Row 

     for(var i = 0; i < count ; i++){ 
      newRow.find('tr').last().append("<td> Content </td>").attr("contenteditable", true); 
// Fill the cells with a default text and set the property "editable" 
     } 
    } 

だから私の質問は、私は、各セルが編集可能であることを、コードを書くことができるか、でしょうか?クリックすると、行全体が編集可能になります。各セルはそのプロパティを持つ必要があります。

私は助けることができるコードが見つかりました:

//$('table th:nth-child(4)').attr("contenteditable", true) 

これは、第四ヘッダ/セルの編集可能になりますが、どのように私はそれを使用することができ、それぞれの新しい作成したヘッダ/セルは、n番目の子でありますか?

+0

で働いていた:これに

newRow.find('tr').last().append("<td> Content </td>").attr("contenteditable", true); 

:だから、この行を変更.append()を使用してhtml文字列を挿入すると、文字列 'コンテンツ' .append()の後の最後の行を返すので、contenteditableをその行の新しい要素の最初の行に設定します。 – Shilly

答えて

0

jQuery append関数は、新しい[appended]要素を返さず、追加された要素を返します。したがって、コード内のエラーが返されます。それにかかわらず、追加文字列に属性を手動で設定するほうが簡単です。私はあなたのため、何のjqueryの専門家だが、

newRow.find('tr').last().append("<td contenteditable="true"> Content </td>") 

はそれを

0

を行う必要がありますそれは

$('table td').last().attr("contenteditable", true); 
関連する問題