2017-08-05 19 views
1

このテーブルの「開始日」列のinnerTextを変更したいとします。テーブルの特定の列の内容を変更する方法は?

<tr id="currentRow"> 
    <th>Name</th> 
    <th>Position</th> 
    <th>Office</th> 
    <th>Age</th> 
    <th>Start date</th> 
    <th>Salary</th>   
</tr> 

私は以下のコードを試してみましたが、それはうまくいきませんでした:

ここではコードです。

document.getElementById("currentRow").td[4] = "this is a text"; 

どうすれば実現できますか?

答えて

0

あなたはquerySelectorAllを使用して、選択した要素の内容を変更する場合のように、.innerHTMLを追加する必要があります。

document.querySelectorAll('#currentRow th')[4].innerHTML = "this is a text"; 

注:あなたのテーブルには、少なくとも1 tdが含まれていることを確認してください。

これが役に立ちます。

+0

ただし、getElementByIdが機能していないため、querySelectorAllは機能しています。 – djmayank

+0

はおそらくtheres no .tdプロパティを引き起こしますか? :/ –

+0

はい、 'td'要素がなければ動作しません。 –

0

<table> 
 
<tr id="currentRow"> 
 
    <th>Name</th> 
 
    <th>Position</th> 
 
    <th>Office</th> 
 
    <th>Age</th> 
 
    <th>Start date</th> 
 
    <th>Salary</th>   
 
</tr> 
 
</table> 
 
<script> 
 
document.getElementById("currentRow").children[4].innerHTML = "this is a text"; 
 
</script>

document.querySelectorAll('#currentRow th')[4].innerHTML = "this is a text";
<table> 
 
    <tr id="currentRow"> 
 
    <th>Name</th> 
 
    <th>Position</th> 
 
    <th>Office</th> 
 
    <th>Age</th> 
 
    <th>Start date</th> 
 
    <th>Salary</th> 
 

 
    </tr> 
 
</table>

Theresの要素でないTDむしろ子供アレイ。

+0

ありがとう!これは機能します。 – kreatusJohn

+0

@ user3080440助けてうれしい;)この質問に答えると、今はあなたの仕事がこれらの2つの回答のうちの1つに緑色のチェックマークを打つことになります(ザカリアが最初です、ちょうどスティンです) –

関連する問題