如何在textarea中所选文本下方显示div?

阿努布

我有一种情况,我需要在文本区域中选择文本时显示一个div(如弹出窗口)。但是,我使用鼠标向下拖动,有时div的位置不完全在文本下方。

JavaScript:

    function getSel() {
    // obtain the object reference for the textarea>
    var txtarea = document.getElementById("mytextarea");
    // obtain the index of the first selected character
    var start = txtarea.selectionStart;
    // obtain the index of the last selected character
    var finish = txtarea.selectionEnd;
    //obtain all Text
    var allText = txtarea.value;
    // obtain the selected text
    var sel = allText.substring(start, finish);
    sel = sel.replace(/[\S]/g, "*");
    //append te text;
    var newText = allText.substring(0, start) + sel + allText.substring(finish, allText.length);
    txtarea.value = newText;
    $('#newpost').offset({ top: 0, left: 0 }).hide();
}

$(document).ready(function () {
    var position;
    $('#newpost').hide();
    $('#mytextarea').on('select', function (e) {
        var str = $('#mytextarea').val();
        $('#newpost').offset(position).show();
        var txtarea = document.getElementById("mytextarea");
        var start = txtarea.selectionStart;
        var finish = txtarea.selectionEnd;
        $('#newpost div').text('Replace with stars');
    }).on('select', function (e) {
        position = { top: e.pageY+10, left: e.pageX };
    });
    $('#newpost').hide();
});
function closePopUp() {
    $('#newpost').hide();
}

这是我的朋克链接

在这里,我的要求是显示div的文本选择。但是,当我使用on-select而不是鼠标按下时,div显示在文本区域下方。

提前致谢。

基里尔·西蒙诺夫(Kirill Simonov)

前几天在这个答案,我建议找到光标位置和显示的方法divtextarea当用户选择一些文本。

但是,正如@anub所提到的,这种方法有效,div有时不显示在所选文本的正下方,而是上下几个像素-因为它的位置是根据第一个用户的点击确定的。

简短搜索我发现了这篇文章,描述了如何通过创建div给定文本区域的临时克隆来找到所选文本在文本区域中的位置。

getCursorXY从那里开始采用该方法,并用它来定位弹出窗口。

试一试!

function getSel() {
    // obtain the object reference for the textarea>
    var txtarea = document.getElementById("mytextarea");
    // obtain the index of the first selected character
    var start = txtarea.selectionStart;
    // obtain the index of the last selected character
    var finish = txtarea.selectionEnd;
    //obtain all Text
    var allText = txtarea.value;

    // obtain the selected text
    var sel = Array(finish - start + 1).join("*");
    //append te text;
    var newText = allText.substring(0, start) + sel + allText.substring(finish, allText.length);
    txtarea.value = newText;
    
    $('#newpost').offset({top: 0, left: 0}).hide();
}
function closePopUp() {
    $('#newpost').offset({top: 0, left: 0}).hide();
}

$(document).ready(function () {
    closePopUp();
    var newpost = $('#newpost');
    $('#mytextarea').on('select', function (e) {
        var txtarea = document.getElementById("mytextarea");
        var start = txtarea.selectionStart;
        var finish = txtarea.selectionEnd;
        newpost.offset(getCursorXY(txtarea, start, 20)).show();
        newpost.find('div').text(Array(finish - start + 1).join("*"));
    });
});

const getCursorXY = (input, selectionPoint, offset) => {
  const {
    offsetLeft: inputX,
    offsetTop: inputY,
  } = input
  // create a dummy element that will be a clone of our input
  const div = document.createElement('div')
  // get the computed style of the input and clone it onto the dummy element
  const copyStyle = getComputedStyle(input)
  for (const prop of copyStyle) {
    div.style[prop] = copyStyle[prop]
  }
  // we need a character that will replace whitespace when filling our dummy element 
  // if it's a single line <input/>
  const swap = '.'
  const inputValue = input.tagName === 'INPUT' ? input.value.replace(/ /g, swap) : input.value
  // set the div content to that of the textarea up until selection
  const textContent = inputValue.substr(0, selectionPoint)
  // set the text content of the dummy element div
  div.textContent = textContent
  if (input.tagName === 'TEXTAREA') div.style.height = 'auto'
  // if a single line input then the div needs to be single line and not break out like a text area
  if (input.tagName === 'INPUT') div.style.width = 'auto'
  // create a marker element to obtain caret position
  const span = document.createElement('span')
  // give the span the textContent of remaining content so that the recreated dummy element 
  // is as close as possible
  span.textContent = inputValue.substr(selectionPoint) || '.'
  // append the span marker to the div
  div.appendChild(span)
  // append the dummy element to the body
  document.body.appendChild(div)
  // get the marker position, this is the caret position top and left relative to the input
  const { offsetLeft: spanX, offsetTop: spanY } = span
  // lastly, remove that dummy element
  // NOTE:: can comment this out for debugging purposes if you want to see where that span is rendered
  document.body.removeChild(div)
  // return an object with the x and y of the caret. account for input positioning 
  // so that you don't need to wrap the input
  return {
    left: inputX + spanX,
    top: inputY + spanY + offset,
  }
}
#mytextarea {width: 600px; height: 200px; overflow:hidden; position:fixed}
#newpost {
    position:absolute;
    background-color:#ffffdc;
    border:1px solid #DCDCDC;
    border-radius:10px;
    padding-right:5px; 
    width: auto;
    height: 30px;
}
#newpost span {
    cursor:pointer;
    position: absolute;
    top: 0;
    right: 5px;
    font-size: 22px;
}
#newpost div {
    color:#0000ff;
    padding:10px;
    margin-right:10px;
    width: auto;
    cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
  <head>
  </head>
 <body>
    <textArea id="mytextarea"></textArea>
    <div id="newpost">
        <span onclick="closePopUp();">&#735;</span>
        <div onclick="getSel()"></div>
    </div>
</body>

</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在QTextBrowser中居中显示所选文本

如何获取所选文本并显示它们?

如何更改HTML页面中所选文本的颜色?

如何获得所选文本位置从textarea的在JavaScript?

如何在下拉菜单上显示与所选文本不同的选项文本?

sed:如何在保留所选文本的同时保留所选文本的同时替换文本?

确定ExtendScript中所选文本的“标签”

如何在 ASP.NET 中的 2 行下拉列表中显示所选文本?

如何搜索所选文本?

如何在Notepad ++中检查所选文本的字数?

如何在JTextPane中轻松编辑所选文本的样式?

如何在PHPStorm中将所选文本转换为小写?

如何在PHPStorm中更改所选文本/代码的颜色

如何在Qt中更改所选文本?

如何在 RichTextBox 中获取所选文本的父标签?

如何删除Sublime Text 3中所选文本右边的所有内容?

Android Spinner不显示所选文本

Notepad ++突出显示所选文本的出现

调整jQuery中所选文本的字体大小

更改MS Word宏中所选文本的字体

在不发送表单的情况下显示输入文本的数量等于输入文件中所选文件的数量

jQuery:如何包装所选文本

如何在textarea的下拉列表中编辑所选文件,然后将其保存?

如何在选择框容器下方显示所选列表?

如何显示所选文件的数量

如何在Rich文本框中更改所选文本背景颜色WPF C#

在文本区域中显示所选文本

如何更改所选文本和选择/单词匹配的突出显示?

在Microsoft Word / Excel中焦点更改时如何保持所选文本突出显示