如何在悬停时仅突出显示表格的行,但最后一列除外?

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

How do I only highlight rows of a table on hover except the last column?

问题

问题类似于此帖子:https://stackoverflow.com/questions/41770244/html-table-how-highlight-row-on-hover-except-first-column。然而,这里的回复不是我要的效果。我希望当我悬停在任何一个列上时,前两列都会高亮显示,而当我悬停在第三列上时,不会有任何高亮显示。

英文:

The problem I am facing is similar to this post: https://stackoverflow.com/questions/41770244/html-table-how-highlight-row-on-hover-except-first-column. However the replies here are not the effect that I am looking for. I am looking for both the first 2 columns to light up when I hover over either one, and for the 3rd column to not light up anything at all when I hover over it.

答案1

得分: 1

请不要使用任何 [tag:javascript] 或 [tag:jquery],只需两个 [tag:css] 规则即可。

选择所有 tr:hover,然后通过使用 td:not(:last-of-type) 来排除最后一列。

然后禁用最后一列上的所有 pointer-events,以防止触发 hover 事件。

table>tbody>tr:hover td:not(:last-of-type) {
  background-color: red;
}

table>tbody>tr>td:last-of-type {
  pointer-events: none;
}
英文:

Please don't use any [tag:javascript] nor [tag:jquery] for this, two [tag:css] rule's are enough.

Select all the tr:hover, and then exclude the last col by using td:not(:last-of-type).

Then disable all pointer-events on the last column to disable that triggering the hover.

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

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

table&gt;tbody&gt;tr:hover td:not(:last-of-type) {
  background-color: red;
}

table&gt;tbody&gt;tr&gt;td:last-of-type {
  pointer-events: none;
}

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

&lt;table border cellpadding=&quot;5&quot;&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;F&lt;/td&gt;
      &lt;td&gt;o&lt;/td&gt;
      &lt;td&gt;o&lt;/td&gt;
      &lt;td&gt;B&lt;/td&gt;
      &lt;td&gt;a&lt;/td&gt;
      &lt;td&gt;r&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;F&lt;/td&gt;
      &lt;td&gt;o&lt;/td&gt;
      &lt;td&gt;o&lt;/td&gt;
      &lt;td&gt;B&lt;/td&gt;
      &lt;td&gt;a&lt;/td&gt;
      &lt;td&gt;r&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

答案2

得分: -1

如果您需要在第三列中触发鼠标事件,这将无法在没有JavaScript的情况下完成。

问题是第三列应该被禁用。可以使用pointer-events:none来实现。但如果我们需要在第三列中触发鼠标事件,以下是解决方案:

您可以使用nth-child CSS伪类和一些JavaScript:

https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child

'mousemove mouseleave'.split(' ').forEach(name =>
  $table.addEventListener(name, syncHover)
);

function syncHover(e) {

  $table.querySelector('tr.hover')?.classList.remove('hover');

  const $td = e.target.closest('td');

  $td && [...$td.parentElement.children].indexOf($td) < 2 &&
    $td.parentElement.classList.add('hover');

}
table tr.hover>td:nth-child(-n + 2) {
  background: lightgray;
  cursor: pointer;
}

table td {
  padding: 5px 10px;
}

table {
  border-collapse: collapse;
  background: #eee;
}
<table id="$table">
  <tr>
    <td>col1</td>
    <td>col2</td>
    <td>col3</td>
  </tr>
  <tr>
    <td>col1</td>
    <td>col2</td>
    <td>col3</td>
  </tr>
  <tr>
    <td>col1</td>
    <td>col2</td>
    <td>col3</td>
  </tr>
  <tr>
    <td>col1</td>
    <td>col2</td>
    <td>col3</td>
  </tr>
</table>
英文:

If you need mouse events in the 3rd column this cannot be done without JS.

The problem that the 3rd column should be disabled. It could be done with pointer-events:none. But if we need mouse events in the 3rd columns here's solution:

You could use the nth-child CSS pseudo class and some Javascript:

https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child

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

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

&#39;mousemove mouseleave&#39;.split(&#39; &#39;).forEach(name =&gt;
  $table.addEventListener(name, syncHover)
);

function syncHover(e) {

  $table.querySelector(&#39;tr.hover&#39;)?.classList.remove(&#39;hover&#39;);

  const $td = e.target.closest(&#39;td&#39;);

  $td &amp;&amp; [...$td.parentElement.children].indexOf($td) &lt; 2 &amp;&amp;
    $td.parentElement.classList.add(&#39;hover&#39;);

}

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

table tr.hover&gt;td:nth-child(-n + 2) {
  background: lightgray;
  cursor: pointer;
}

table td {
  padding: 5px 10px;
}

table {
  border-collapse: collapse;
  background: #eee;
}

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

&lt;table id=&quot;$table&quot;&gt;
  &lt;tr&gt;
    &lt;td&gt;col1&lt;/td&gt;
    &lt;td&gt;col2&lt;/td&gt;
    &lt;td&gt;col3&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;col1&lt;/td&gt;
    &lt;td&gt;col2&lt;/td&gt;
    &lt;td&gt;col3&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;col1&lt;/td&gt;
    &lt;td&gt;col2&lt;/td&gt;
    &lt;td&gt;col3&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;col1&lt;/td&gt;
    &lt;td&gt;col2&lt;/td&gt;
    &lt;td&gt;col3&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月6日 18:06:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627700.html
匿名

发表评论

匿名网友

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

确定