有一个函数可以获取页面上选定的文本:
$('*').on('mouseup', function() {
let mytext = getSelectedText();
console.log( mytext );
});
var getSelectedText = function() {
let text = '';
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection) {
text = document.selection.createRange().text;
}
return text;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>I won't beg for your love: it's laid Safely to rest, let the earth settle... Don't expect my jealous letters Pouring in to plague your bride. But let me, nevertheless, advise you: Give her my poems to read in bed, Give her my portraits to keep - it's wise to Be kind like that when newly-wed.</p>
我想让它在选择文本后,在选择结束时创建一个块,我将在其中放置信息。
或多或少是这样的:
我将不胜感激您的帮助!

<div class="block"> Я блок с текстом <div class="close">X</div></div>并将这些元素设置为正常定位,即 块本身是绝对的,关闭按钮例如是相对的,不要忘记在 CSS 显示中隐藏此元素:无if(mytext !== ''),如果是这样,在突出显示文本后获取鼠标坐标一切似乎都是。