【问题标题】:Unselectable content/text within an HTML tableHTML 表格中不可选择的内容/文本
【发布时间】:2016-11-22 02:40:17
【问题描述】:

我有以下html:

<table class="code-table hljs">
  <tbody>
    <tr class="code-row">
      <td class="line-number unselectable">1</td>
      <td class="code-col">one</td>
    </tr>
    <tr class="code-row">
      <td class="line-number unselectable">2</td>
      <td class="code-col">two</td>
    </tr>
    <tr class="code-row">
      <td class="line-number unselectable">3</td>
      <td class="code-col">three</td>
    </tr>
  </tbody>
</table>

相关的css:

.unselectable {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

在 Firefox 和 Chrome 中,如果我选择所有 (ctrl-a),我期望发生的情况是,只会选择“一”、“二”和“三”,而不是行号。但是,当我粘贴剪贴板中的内容时,我会得到不同的结果:

Chrome 输出:

one
2 two
3 three

火狐输出:

one

two

three

因此,Chrome 会复制除第一行之外的所有不可选择的行,而 Firefox 会在不应该存在的地方添加一个额外的行。

当前 Chrome 版本为 54.0.2840.71 m,当前 firefox 版本为 49.0.2(根据http://caniuse.com/#feat=user-select-none,两者都可以使用user-select: none;

目前是否可以使用 css 解决方案?

编辑 请注意,我收到的表正在由另一个库呈现,我真的只能操作类。

【问题讨论】:

标签: html css


【解决方案1】:

目前是否可以使用 css 解决方案?

对此的 HTML 解决方案是可能的:

<ol>
<li>one</li>
<li>two</li>
<li>three</li>
</ol>

【讨论】:

  • 行号仍然需要出现在被选中内容的左侧。
  • 是的 - 他们确实如此。我已经把代码移到了sn-p中,大家可以看看。
  • 哈,当然。不幸的是,HTML 表正在由另一个库呈现,我只能控制这些类。但是赞成,我会在问题中添加该注释。
【解决方案2】:

@Rounin 几乎做到了。这是他所做的修改版本。它将显示突出显示 1-3 但它不会复制。

.unselectable {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  visibility: hidden;
  display: block;
 }


 .code-table {
 counter-reset: code-table;
 }

 .code-table tr td:nth-of-type(2)::before {
 counter-increment: code-table;
 content: counter(code-table) '  \00a0\00a0';
 }
<table class="code-table hljs">
  <tbody>
    <tr class="code-row">
      <td class="line-number unselectable">1</td>
      <td class="code-col">one</td>
    </tr>
    <tr class="code-row">
      <td class="line-number unselectable">2</td>
      <td class="code-col">two</td>
    </tr>
    <tr class="code-row">
      <td class="line-number unselectable">3</td>
      <td class="code-col">three</td>
    </tr>
  </tbody>
</table>

【讨论】:

  • 它只是看起来像在 chrome 中突出显示它们,它不会复制。
  • 您也可以在 javascript 中轻松完成。只是一个建议。
  • 我保证的最后一件事,'\00a0\00a0' 只是空格,令我困扰的是数字数字接近于它们的文本对应物。
  • 感谢 Branded,我会看看我是否也可以实现这个。已投赞成票,在我接受之前会留下更多答案。
  • 听起来不错,如果您需要其他任何内容或不明白的内容,请随时提问。
【解决方案3】:

您可以删除.code-table 的第一列

visibility: collapse;

然后您可以在.code-table 的第二列中部署一个包含 CSS 计数器的 ::before 伪元素来解决此问题:

.code-table {
counter-reset: code-table;
}

.code-table tr td:nth-of-type(1) {
visibility: collapse;
}

.code-table tr td:nth-of-type(2)::before {
counter-increment: code-table;
content: counter(code-table) ': ';
}
<table class="code-table hljs">
  <tbody>
    <tr class="code-row">
      <td class="line-number unselectable">1</td>
      <td class="code-col">one</td>
    </tr>
    <tr class="code-row">
      <td class="line-number unselectable">2</td>
      <td class="code-col">two</td>
    </tr>
    <tr class="code-row">
      <td class="line-number unselectable">3</td>
      <td class="code-col">three</td>
    </tr>
  </tbody>
</table>

【讨论】:

  • 破解了:visibility: collapse;
  • 另一方面...在 2013 年 3 月关于 CSS-Tricks (css-tricks.com/almanac/properties/v/visibility) 的客座文章中,Sara Cope 检查了 visibility: collapse; 的不一致浏览器处理并警告:不要永远使用这个
猜你喜欢
  • 2011-01-12
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 1970-01-01
  • 1970-01-01
  • 2021-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多