简体   繁体   English

在两个INS标记中双击单词JavaScript window.getSelection

[英]Double click word JavaScript window.getSelection in two INS tag

My HTML is: 我的HTML是:

<p><ins data-id="1">111</ins><ins data-id="2">222</ins></p>

The output of this code is: 此代码的输出是:

在此输入图像描述

If I double click the word, it's selecting the complete word, like this: 如果我双击该单词,它会选择完整的单词,如下所示:

在此输入图像描述

But I want to select the letters based on the INS tag data-id 但我想根据INS标签data-id选择字母

Ex:- if I double click the 111 I want to select only 111 like this: 例如: - 如果我双击111我想只选择111这样:

在此输入图像描述

How to modify the default double click selection to JavaScript selection? 如何修改默认的双击选择到JavaScript选择?

I tried the following code: 我尝试了以下代码:

var containerid = $(e.currentTarget);
if (window.getSelection) {
   var range = document.createRange();
   range.selectNode(containerid);
   var sel = window.getSelection()
   sel.removeAllRanges();
   sel.addRange(range);
}

But it's not working as expected. 但它没有按预期工作。

 <p><ins data-id="1">111</ins>&#8203;<ins data-id="2">222</ins></p> 

你可以在它们之间放置一个零宽度空间&#8203;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM