簡體   English   中英

如何在textarea中顯示選定文本下方的div?

[英]how to show a div below selected text in textarea?

我有一個場景,我需要在文本區域中顯示一個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();
}

這是我的plunker鏈接

這里我的要求是顯示文本的div選擇。但是當我使用on-select而不是mouse-down時,div顯示在text-area下面。

提前致謝。

幾天前,在這個答案中,我提出了一種方法,即當用戶選擇一些文本時,找到光標位置並在textarea上顯示div

然而,這種方法有效,正如@anub所提到的, div有時顯示在所選文本的正下方,但是上下兩個像素 - 因為它的位置是根據第一個用戶的點擊確定的。

經過短暫的搜索,我發現這篇文章描述了如何通過創建給定textarea的臨時div克隆來找到textarea中所選文本的位置。

我從那里采用了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> 

我在關閉彈出窗口並重新選擇另一個區域時遇到了這個問題,我解決了這個問題

function closePopUp() {
    $('#newpost').offset({ top: 0, left: 0 }).hide();
}

看一下這個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM