将类添加到表行没有效果

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

Adding class to table row has no effect

问题

以下是您要翻译的代码部分:

document.querySelector('#tableEvents').onclick = function(e) {
  let index = e.target.parentElement.rowIndex;
  let row = e.target.parentElement;
  row.classList.add('table-warning');
  console.log(index, row.classList.toString());
}
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" />

<table id="tableEvents">
  <thead>
    <tr>
      <th scope="col">Col 1</th>
      <th scope="col">Col 2</th>
      <th scope="col">Col 3</th>
    </tr>
  </thead>
  <tbody class="table-group-divider">
    <tr>
      <td>1</td>
      <td>a</td>
      <td>1a</td>
    </tr>
    <tr>
      <td>2</td>
      <td>b</td>
      <td>2b</td>
    </tr>
    <tr>
      <td>3</td>
      <td>c</td>
      <td>3c</td>
    </tr>
    <tr>
      <td>4</td>
      <td>d</td>
      <td>4d</td>
    </tr>
  </tbody>
</table>

请注意,这是您提供的JavaScript和HTML代码的翻译。如果您需要进一步的帮助或解释,请告诉我。

英文:

I'm trying to do two things in pure Javascript when the user clicks a table:

  1. return the number of the row (it works)
  2. change the background of the row

Here my current code:

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

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

document.querySelector(&#39;#tableEvents&#39;).onclick = function(e) {
  let index = e.target.parentElement.rowIndex;
  let row = e.target.parentElement;
  row.classList.add(&#39;table-warning&#39;);
  console.log(index, row.classList.toString());
}

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

&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;

&lt;table id=&quot;tableEvents&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th scope=&quot;col&quot;&gt;Col 1&lt;/th&gt;
      &lt;th scope=&quot;col&quot;&gt;Col 2&lt;/th&gt;
      &lt;th scope=&quot;col&quot;&gt;Col 3&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody class=&quot;table-group-divider&quot;&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;a&lt;/td&gt;
      &lt;td&gt;1a&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;b&lt;/td&gt;
      &lt;td&gt;2b&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;c&lt;/td&gt;
      &lt;td&gt;3c&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;d&lt;/td&gt;
      &lt;td&gt;4d&lt;/td&gt;
    &lt;/tr&gt;

  &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

As you can see, it returns the correct index but no changes are made to the row.
Reading the docs I understand it should be enough to add the table-warning class (for example) to the &lt;tr&gt; tag.

It actually adds the class (the console shows the classList value) but no color is shown.

The second issue is how to remove the class from the other rows. In jQuery I would do:

$(this).addClass(&#39;table-warning&#39;).siblings().removeClass(&#39;table-warning&#39;);

but I don't understand how to convert the siblings() call in JavaScript.
Do I need to traverse the whole table, removing the class from each row manually?

答案1

得分: 4

现在只需循环所有同级行,除了那一行,然后移除类,不需要 jQuery(这里有一些基于纯 JS 的方法:https://stackoverflow.com/questions/842336/is-there-a-way-to-select-sibling-nodes)

另外,您需要为 table-warning 辅助类添加 table 类:

<table class="table" id="tableEvents">
  <thead>
    <tr>
      <th scope="col">Col 1</th>
      <th scope="col">Col 2</th>
      <th scope="col">Col 3</th>
    </tr>
  </thead>
  <tbody class="table-group-divider">
    <tr>
      <td>1</td>
      <td>a</td>
      <td>1a</td>
    </tr>
    <tr>
      <td>2</td>
      <td>b</td>
      <td>2b</td>
    </tr>
    <tr>
      <td>3</td>
      <td>c</td>
      <td>3c</td>
    </tr>
    <tr>
      <td>4</td>
      <td>d</td>
      <td>4d</td>
    </tr>
  </tbody>
</table>
英文:

Now just loop all the sibling rows except that one, and remove the class, no jQuery needed (here's a few based upon approaches how to do it with vanilla JS: https://stackoverflow.com/questions/842336/is-there-a-way-to-select-sibling-nodes)

Also, you need to add table class for the table-warning helper class to work:

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

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

document.querySelector(&#39;#tableEvents&#39;).onclick = function(e) {
  let index = e.target.parentElement.rowIndex;
  // skip header
  if(index===0 || !index) return;
  
  let row = e.target.parentElement;
  row.classList.add(&#39;table-warning&#39;);
  console.log(index, row.classList.toString());
      
	let siblingRow = row.parentNode.firstChild;

	
	while (siblingRow) {

		if (siblingRow.nodeType === 1 &amp;&amp; siblingRow !== row) {

			siblingRow.classList.remove(&#39;table-warning&#39;);
      
		}
		siblingRow = siblingRow.nextSibling;
	}
  
 
}

<!-- language: lang-css -->

.as-console-wrapper { max-height: 20% !important; bottom: 0; }

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

&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;

&lt;table class=&quot;table&quot; id=&quot;tableEvents&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th scope=&quot;col&quot;&gt;Col 1&lt;/th&gt;
      &lt;th scope=&quot;col&quot;&gt;Col 2&lt;/th&gt;
      &lt;th scope=&quot;col&quot;&gt;Col 3&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody class=&quot;table-group-divider&quot;&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;a&lt;/td&gt;
      &lt;td&gt;1a&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;b&lt;/td&gt;
      &lt;td&gt;2b&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;c&lt;/td&gt;
      &lt;td&gt;3c&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;d&lt;/td&gt;
      &lt;td&gt;4d&lt;/td&gt;
    &lt;/tr&gt;

  &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月27日 18:44:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579439.html
匿名

发表评论

匿名网友

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

确定