如何用contentEditable = true选择两个元素的文本?

时间:2016-02-17 23:22:03

标签: javascript html5

我想问一下,如何选择两个HTML元素的文本,哪个属性contentEditable设置为true。当我开始在一个元素中选择带有鼠标的文本时,选择将在其范围内结束。永远不要到达第二个元素。

我也尝试应用“user-select”属性,但没有效果。

这是片段。

<body>
<div contentEditable = true style="position:absolute; left: 10px; top:10px; width:100px; height:200px; border: 3px solid #73AD21;">
Donec tempus, nisi a pharetra placerat, diam nisi aliquam elit, a consectetur magna enim sed ligula. 
</div>

<div contentEditable = true style="position:absolute; left: 130px; top:10px; width:100px; height:200px; border: 3px solid #73AD21;">
The oldest browsers support only one way of registering event handlers, the way invented by Netscape. 
</div>
</body>

thanx很多

1 个答案:

答案 0 :(得分:3)

您可以将这些<div>包装在另一个<div>内,并将其设为contentEditable="true"而不是这两个。

<body>
  <div contentEditable="true">
    <div style="position:absolute; left: 10px; top:10px; width:100px; height:200px; border: 3px solid #73AD21;">
Donec tempus, nisi a pharetra placerat, diam nisi aliquam elit, a consectetur magna enim sed ligula.</div>
    <div style="position:absolute; left: 130px; top:10px; width:100px; height:200px; border: 3px solid #73AD21;">
The oldest browsers support only one way of registering event handlers, the way invented by Netscape.</div>
  </div>
</body>

JS Bin here