可以在不给每个元素分配ID的情况下替换HTML表的第一行所有内容吗?

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

Is it possible to replace all the contents of the first row of an HTML table without assigning an ID to each <td> element?

问题

You can achieve your goal by selecting all the <td> elements within the first row and then updating their content individually. Here's the modified JavaScript code to do that:

function fn() {
    var firstRow = document.querySelector('table tr');
    var customText = ["New Name", "newemail@example.com"]; // Replace with your custom text
    var tdElements = firstRow.querySelectorAll('td');

    for (var i = 0; i < customText.length; i++) {
        tdElements[i].textContent = customText[i];
    }
}

This code selects the first row of the table, then selects all the <td> elements within it and updates their content individually with your custom text when the button is clicked.

英文:

I have a simple html table consisting table headers and a first row as shown below:

&lt;html&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;email&lt;/th&gt;
&lt;/head&gt;
&lt;tr&gt;
&lt;td&gt;Kipchoge&lt;/td&gt;
&lt;td&gt;k@gmail.com&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;input type=&quot;button&quot; id=&quot;btn&quot; value=&quot;btn&quot; onClick=&quot;fn()&quot;/&gt;
&lt;/html&gt;

How can I change all the contents of the first row with my custom text when I click the button?
I have tried assigning my new text directly to document.getElementByTagName(&quot;tr&quot;)[1].innerHTML with no success as shown in the code below:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function fn(){
var d = document.getElementsByTagName(&#39;tr&#39;);
var customtext = &quot;&quot;;
d[1].innerHTML = customtext;
}

<!-- language: lang-html -->

&lt;table&gt;
&lt;thead&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;email&lt;/th&gt;
&lt;/head&gt;
&lt;tr&gt;
&lt;td&gt;Kipchoge&lt;/td&gt;
&lt;td&gt;k@gmail.com&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;input type=&quot;button&quot; id=&quot;btn&quot; value=&quot;btn&quot; onClick=&quot;fn()&quot;/&gt;

<!-- end snippet -->

I know that what I have done in the above code is trying to assign a new value to an existing innerHTML value which can be easily achieved when you use getElementById, but my idea is to change all the values of the first row once without assigning an id to each &lt;td&gt;&lt;/td&gt;.
Is there an alternative approach? Or what should I do to my approach in order to solve such a problem?

I tried changing existing innerHTML with new text as shown below:

d[1].innerHTML = customtext;

But that did not work.

答案1

得分: 1

getElementsByTagName 函数返回一个 活动的 HTMLCollection,它是一个类似数组的元素集合。在你的代码中,你试图在表格行节点内添加文本。表格行 - 或 tr - 只允许包含 td 元素。在下面的示例中,我使用 innerHTML 来设置包含带有文本的 td 的 HTML。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
function fn(){
  var tr = document.getElementsByTagName('tr')[0];
  tr.innerHTML = '<td>Text 1</td><td>Text 2</td>';
}

<!-- language: lang-html -->
<table>
  <thead>
    <th>name</th>
    <th>email</th>
  </thead>
  <tr>
    <td>Kipchoge</td>
    <td>k@gmail.com</td>
  </tr>
</table>

<input type="button" id="btn" value="btn" onClick="fn()"/>
</html>

<!-- end snippet -->
英文:

The function getElementsByTagName returns a live HTMLCollection which is an array-like-object of elements. In your code you are trying to add text inside a table row node. Table row - or tr - is only allowed to have td elements inside it. In the following example, I am using innerHTML to set HTML that represents the that contains the td with the text inside them.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function fn(){
  var tr = document.getElementsByTagName(&#39;tr&#39;)[0];
  tr.innerHTML = `&lt;td&gt;Text 1&lt;/td&gt;&lt;td&gt;Text 2&lt;/td&gt;`;
}

<!-- language: lang-html -->

&lt;table&gt;
  &lt;thead&gt;
    &lt;th&gt;name&lt;/th&gt;
    &lt;th&gt;email&lt;/th&gt;
  &lt;/thead&gt;
  &lt;tr&gt;
    &lt;td&gt;Kipchoge&lt;/td&gt;
    &lt;td&gt;k@gmail.com&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

&lt;input type=&quot;button&quot; id=&quot;btn&quot; value=&quot;btn&quot; onClick=&quot;fn()&quot;/&gt;
&lt;/html&gt;

<!-- end snippet -->

答案2

得分: 0

使用更强大的 querySelector 方法,通过使用 CSS 选择器从 DOM 中选择元素。在您的情况下,您可以使用 tbody tr:first-of-type 选择第一行。

const firstTableRow = document.querySelector('tbody tr:first-of-type');

firstTableRow.cells[0].textContent = 'Foo';
firstTableRow.cells[1].textContent = 'foo@bar.com';
<table>
  <thead>
    <th>name</th>
    <th>email</th>
  </thead>

  <tbody>
    <tr>
      <td>Kipchoge</td>
      <td>k@gmail.com</td>
    </tr>
    
    <tr>
      <td>Kipchoge</td>
      <td>k@gmail.com</td>
    </tr>
  </tbody>
</table>

这是您提供的代码的翻译部分。

英文:

Use the more powerful querySelector method to select elements from the DOM by using CSS Selectors. In your case you can select the first row with tbody tr:first-of-type.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const firstTableRow = document.querySelector(&#39;tbody tr:first-of-type&#39;);

firstTableRow.cells[0].textContent = &#39;Foo&#39;;
firstTableRow.cells[1].textContent = &#39;foo@bar.com&#39;;

<!-- language: lang-html -->

&lt;table&gt;
  &lt;thead&gt;
    &lt;th&gt;name&lt;/th&gt;
    &lt;th&gt;email&lt;/th&gt;
  &lt;/thead&gt;

  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Kipchoge&lt;/td&gt;
      &lt;td&gt;k@gmail.com&lt;/td&gt;
    &lt;/tr&gt;
    
    &lt;tr&gt;
      &lt;td&gt;Kipchoge&lt;/td&gt;
      &lt;td&gt;k@gmail.com&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

答案3

得分: 0

你可以以这种方式遍历表格标题:

&lt;html&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;th&gt;姓名&lt;/th&gt;
&lt;th&gt;电子邮件&lt;/th&gt;
&lt;/thead&gt;
&lt;tr&gt;
&lt;td&gt;Kipchoge&lt;/td&gt;
&lt;td&gt;k@gmail.com&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;input type=&quot;button&quot; id=&quot;btn&quot; value=&quot;按钮&quot; onClick=&quot;fn()&quot;/&gt;
&lt;/html&gt;

// 你可以选取 thead 并获取其中所有的 th 元素

const head = document.getElementById(&quot;thead-id&quot;) /* 或者 */ document.querySelector(&#39;thead&#39;)
// 如果你只想获取 thead 中第一行的 th(假设 thead 中有多行)
const row = document.querySelector(&quot;thead tr:first-of-type&quot;) /* 或者 */ document.querySelector(&quot;#thead-id tr:first-of-type&quot;)

const thList = head.querySelectorAll(&quot;th&quot;);
// 或者
const thList = row.querySelectorAll(&quot;th&quot;);
// 现在 th 列表就像一个列表或数组,你可以通过索引选择项目,或者运行 forEach 循环
thList.forEach((th) =&gt; {
th.innerText = &quot;自定义文本&quot;
})
英文:

You can iterate over the table heads in this way

&lt;html&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;email&lt;/th&gt;
&lt;/head&gt;
&lt;tr&gt;
&lt;td&gt;Kipchoge&lt;/td&gt;
&lt;td&gt;k@gmail.com&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;input type=&quot;button&quot; id=&quot;btn&quot; value=&quot;btn&quot; onClick=&quot;fn()&quot;/&gt;
&lt;/html&gt;


// you can target thead and get all ths that are present inside it

const head = document.getElementById(&quot;thead-id&quot;) /* or */ document.querySelector(&#39;thead&#39;)
// if you don&#39;t want all th in thead only the ones in first row of the thead
//(incase you have more than one rows in thead)
const row = document.querySelector(&quot;thead tr:first-of-type&quot;) /* or */ document.querySelector(&quot;#thead-id tr:first-of-type&quot;)

const thList = head.querySelectorAll(&quot;th&quot;);
// or 
const thList = row.querySelectorAll(&quot;th&quot;);
// Now th list acts like a list or an array where you can target items using index or run a forEach loop
thList.forEach((th) =&gt; {
th.innerText = &quot;customText&quot;
})



</details>



huangapple
  • 本文由 发表于 2023年5月29日 16:28:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355764.html
匿名

发表评论

匿名网友

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

确定