突出显示/选择多个带有内容可编辑范围的 div?

发布于 2024-10-14 08:24:40 字数 287 浏览 3 评论 0原文

假设我有一组 contenteditable="true" div。

<div id="0" contenteditable="true"></div>
<div id="1" contenteditable..></div>
<div...etc></div>

我不能有一个div,多个div是必须的。我如何突出显示多个div的内容?使用范围?还要别的吗?

Let's say I have a set of contenteditable="true" divs.

<div id="0" contenteditable="true"></div>
<div id="1" contenteditable..></div>
<div...etc></div>

I can't have one div, multiple divs is a must. How could I highlight the content of more than one div? Using ranges? Anything else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

说不完的你爱 2024-10-21 08:24:40

答案是这取决于浏览器。请参阅此示例,了解使用范围的两种方法的测试。第一个尝试为每个可编辑

创建一个范围,并将它们全部添加到选择中。第二个尝试创建一个包含两个可编辑

内容的单个 Range。

结果:

  • 在所有浏览器中,用户不可能创建存在于多个可编辑元素中的选择;
  • Firefox 是主要浏览器中最宽松的。两种编程方法都有效。
  • Safari 和 Chrome 是最不宽容的:这两种方法都不会从多个可编辑元素中选择内容。
  • Opera 11 不支持选择多个范围,但支持跨越多个可编辑元素的选定范围。
  • IE 9 之前的版本不支持 DOM Range 或与其他浏览器相同的 Selection API,但等效的 TextRange 代码不允许从多个可编辑元素中进行选择。

The answer is that it depends on the browser. See this example for a test of two methods using Ranges. The first attempts to create a Range per editable <div> and add all of them to the selection. The second attempts to create a single Range encompassing the contents of two editable <div>s.

Results:

  • In all browsers, it is impossible for the user to create a selection that lives in more than one editable element;
  • Firefox is the most permissive of the major browsers. Both programmatic methods work.
  • Safari and Chrome is the least permissive: neither method selects content from more than one editable element.
  • Opera 11 does not support multiple Ranges in the selection, but does support a selected Range spanning more than one editable element.
  • IE pre-version 9 does not support DOM Range or the same Selection API as other browsers, but the equivalent TextRange code does not allow selection from more than one editable element.
萌能量女王 2024-10-21 08:24:40

在其中一个 div 中开始选择后,可以将 div 即时切换为 contenteditable="false"。这样的东西给你的想法(使用JQuery):

$('div').bind("selectstart", function() {
    $('div').attr("contenteditable", false);
});​

Here's a fiddle to demo(仅在Chrome中测试)。

请注意,在小提琴示例中,第一个 ContentEditable Div 获得焦点。这允许您像平常一样打字,但是一旦您使用鼠标或键盘选择任何内容,您就会看到可以像往常一样在 div 之间扩展选择。

这显然需要充实以与多个浏览器一起使用并适当地返回到 contenteditable="true" 。但我认为这个方法是有效的。

It is possible to switch the divs to contenteditable="false" on the fly as a consequence of starting a selection in one of them. Something like this gives you the idea (using JQuery):

$('div').bind("selectstart", function() {
    $('div').attr("contenteditable", false);
});​

Here's a fiddle to demonstrate (only tested in Chrome).

Note in the fiddle example that the first ContentEditable Div gets the focus. This allows you to type away as normal, but as soon as you select anything, using mouse or keyboard, you'll see you can extend the selection across divs as usual.

This obviously needs fleshing out to work with multiple browsers and to turn back to contenteditable="true" appropriately. But I think the approach is valid.

内心激荡 2024-10-21 08:24:40

另一种解决方案是打开父元素的 contentaditable,而不是在每个子元素上将其关闭。

const editableDivs = document.getElementsByClassName("editable")
let selecting = false

for (const editableDiv of editableDivs) {
  editableDiv.addEventListener("mouseleave", (event) => {
    if (selecting)
      document.body.setAttribute("contenteditable", "true")
  })
}

document.body.addEventListener("mousedown", (event) => {
  selecting = true
})

document.body.addEventListener("mouseup", (event) => {
  document.body.setAttribute("contenteditable", "false")
  selecting = false
})
<div class="editable" contenteditable="true">
  This is the first editable div.
</div>

<div class="editable" contenteditable="true">
  This is the second editable div.
</div>

Another solution is to turn on the contentaditable of the parent element instead of turning it off on each child.

const editableDivs = document.getElementsByClassName("editable")
let selecting = false

for (const editableDiv of editableDivs) {
  editableDiv.addEventListener("mouseleave", (event) => {
    if (selecting)
      document.body.setAttribute("contenteditable", "true")
  })
}

document.body.addEventListener("mousedown", (event) => {
  selecting = true
})

document.body.addEventListener("mouseup", (event) => {
  document.body.setAttribute("contenteditable", "false")
  selecting = false
})
<div class="editable" contenteditable="true">
  This is the first editable div.
</div>

<div class="editable" contenteditable="true">
  This is the second editable div.
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文