html - 限制文本选择区域

标签 html css selection

例如,在 GitHub 上,如果您选择代码,它只会选择代码,而不是其旁边的行号:

Text selection limit

这是因为他们使用表格 - td 中的行号和 td 中的代码。

如何使用 div 实现此目的?

编辑:请参阅www.duncannz.com并尝试突出显示文本。我希望选择的内容仅位于内容区域内,而不是填充页面的宽度(例如选择背景不会超过黑色边框)

最佳答案

示例:http://jsfiddle.net/gfp28/

<div>

  <ul class='no_select'>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
  </ul>

  <ul>
      <li>111111111</li>
      <li>222222222</li>
      <li>333333333</li>
      <li>444444444</li>
      <li>555555555</li>
  </ul>

</div>​

CSS:

ul {
  display: inline; 
  float: left; 
  margin: 10px;
}

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

如果将其添加到 CSS 中,它应该将突出显示限制为容器的宽度:

#actualcontent, #actualcontent * { position: relative; }

关于html - 限制文本选择区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12335344/

相关文章:

jquery - 为 jQuery 可排序列表中的每个 <li>(包括嵌套)创建 ID

javascript - 如何在纯 JavaScript 中向元素添加和删除事件类

JQuery 显示基于当前部分 ID 的下拉菜单

xml - XPath:选择文本节点

mysql - 我如何限制MySQL中的选择?

php - 统计包含 X 和检查日期的 SQL 条目

css - html5中的touch silde和mouse scroll有什么不同?

html - 使用CSS更改圆 Angular 按钮的颜色

css - eclipse e4 深色主题 : How to style widgets created by FormToolKit with css

wpf - 仅在 RichTextBox 中为选择添加下划线