无法在JavaScript中将类属性传递给td标签的innerhtml

huangapple go评论98阅读模式
英文:

Not able to pass class attribute in innerhtml for td tag Javascript

问题

我已经开始学习HTML和JavaScript,并希望创建一些动态行。但是每当我尝试使用td标签以及类属性来设置cell.innerHTML时,我可以添加行,但类属性没有被设置。

输出

尝试查看不同的Stack Overflow 链接:来源1

使用不同的标签(如p标签)进行类似尝试,对我来说是有效的。

期望学习如何在设置td标签时传递类属性。

英文:

I have started working on html and javascript and wanted to have some dynamic rows. But whenever I try to set cell.innerHTML with td tag along with class attributes, I am able to add rows but the class attribute is not getting set.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. table, thead, td {
  6. border:1px solid black;
  7. }
  8. .editMe{
  9. color:red;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <button onclick="myFunction()">Try it</button>
  15. </br></br>
  16. <table id="simpleEditableTable" >
  17. <thead>
  18. <tr>
  19. <th>testId</th>
  20. </tr>
  21. </thead>
  22. <tbody>
  23. <tr>
  24. <td class="editMe">1.01</td>
  25. </tr>
  26. <tr>
  27. <td class="editMe">1.02</td>
  28. </tr>
  29. </tbody>
  30. </table>
  31. <p id="para">See</p>
  32. <script>
  33. function myFunction() {
  34. //document.getElementById("demo").innerHTML = '<td style="color:red">helo</td>';
  35. var table = document.getElementById("simpleEditableTable");
  36. var row = table.insertRow(-1);
  37. var cell = row.insertCell(0);
  38. cell.innerHTML='<td class="editMe">Not working</td>';
  39. var para = document.getElementById("para");
  40. para.innerHTML='<p style="color:red">working</p>';
  41. //tried below ones too
  42. // cell.innerHTML="<td class='editMe'>helo</td>";
  43. }
  44. </script>
  45. </body>
  46. </html>

Output

Tried going through different stackover flow links: Source1

Tried similar with different tags like p tag and its working for me.

Expecting to learn how can I pass class attribute in td tag while setting.

答案1

得分: 0

你需要设置单元格的 outerHTML 或行的 innerHTML 来修改 <td> 元素本身。

  1. cell.outerHTML = '<td class="editMe">Some text</td>';

在这里,更适合设置 textContent 并使用 .classList.add 来更改类。

  1. let cell = row.insertCell(0);
  2. cell.classList.add('editMe');
  3. cell.textContent = 'Some text';
英文:

You would need to set the cell's outerHTML or the row's innerHTML to modify the &lt;td&gt; element itself.

  1. cell.outerHTML = &#39;&lt;td class=&quot;editMe&quot;&gt;Some text&lt;/td&gt;&#39;;

Here, it is more appropriate to set the textContent and use .classList.add to change the class.

  1. let cell = row.insertCell(0);
  2. cell.classList.add(&#39;editMe&#39;);
  3. cell.textContent = &#39;Some text&#39;;

答案2

得分: 0

cell.innerHTML= <td class="editMe">Not working</td> ;

Use this `` instead not the ‘’ to enclose your text, since it’s an html tag and not a string

英文:

cell.innerHTML= &lt;td class=&quot;editMe&quot;&gt;Not working&lt;/td&gt; ;

Use this `` instead not the ‘’ to enclose your text, since it’s an html tag and not a string

huangapple
  • 本文由 发表于 2023年3月10日 00:18:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定