简体   繁体   English

在工具栏中选择文本后的HTML工具提示 <input> 或在 <textarea>

[英]HTML Tooltip after selecting text in an <input> or in a <textarea>

Ok, so basically what I need is that when I select some text in an <input> or in a <textarea> , a tooltip appears with two buttons, and then get the selected text in a Javascript variable. 好的,基本上我需要的是,当我在<input><textarea>选择一些文本时,工具提示会出现两个按钮,然后在Javascript变量中获取所选文本。

The image below is an example of what I need: 下图是我需要的示例:

var selectedText = "s is an in"

I'm using Powertip for the tooltip and Rangy for manipulating selections in <input> and <textarea> , the problem is that I can't manage to popup the tooltip and get the selected text in a JS variable after I select the text with the cursor. 我使用Powertip工具提示和瘦长的操纵选择<input><textarea> ,问题是,我不能设法弹出提示并获得一个JS变量中选定的文本后,我选择与文本光标。

Any suggestions on how to achieve this? 关于如何实现这一目标的任何建议?

Thanks in advance. 提前致谢。

UPDATE: 更新:

Thanks to Todd answer, I'm sharing what I was trying to achieve: JSFiddle 感谢Todd的回答,我在分享我要实现的目标: JSFiddle

This seems to work 这似乎有效

$('input, textarea').powerTip({ manual: true }); // have to prevent default plugin


$('input, textarea').on({
    'select': function() {
        var selectedText = window.getSelection().toString();
        $(this).powertip('show');
     },
     'blur': function() {
        $.powerTip.hide();
     }
});

You can add mouse up event on selection of a text and can get the selected text, after that when you will show the selected text, you can also show the tool-tip with that. 您可以在选择文本时添加鼠标向上事件,并可以获取所选文本,此后,当您显示所选文本时,还可以显示带有该提示的工具提示。

Here is the JSFiddle Link for showing the text selection, you can update this and can also show the tool-tip where I have printed the selected text. 这是用于显示文本选择的JSFiddle Link ,您可以对其进行更新,也可以显示我在其中打印所选文本的工具提示。

How to show tool-tip box: 如何显示工具提示框:

Step-1) You need to get the top and left of the input field. 步骤1)您需要在输入字段的顶部和左侧。

Step-2) Create a box using HTML and CSS. 步骤2)使用HTML和CSS创建一个框。

Step-3) Set it's position to absolute and it's container to relative. 步骤3)将其位置设置为绝对位置,并将其容器设置为相对位置。

Step-4) Set it's top and left to that you have received in Step-1. 步骤4)将其设置为您在步骤1中收到的顶部和左侧。

Use a Modal to create popup, create a Text Field Select Event & a Function to trigger open anchor. 使用模态创建弹出窗口,创建文本字段选择事件,然后使用函数触发打开锚点。 Thats all you need. 那就是您所需要的。

here is a working example I have created: 这是我创建的一个工作示例:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Text Select Modal</title>

<style>
    .modalDialog {
    position: fixed;
    font-family: Arial, Helvetica, sans-serif;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(0,0,0,0.8);
    z-index: 99999;
    opacity:0;
    -webkit-transition: opacity 400ms ease-in;
    -moz-transition: opacity 400ms ease-in;
    transition: opacity 400ms ease-in;
    pointer-events: none;
}
.modalDialog:target {
    opacity:1;
    pointer-events: auto;
}

.modalDialog > div {
    width: 300px;
    height:130px;
    position: relative;
    margin: 10% auto;
    padding: 5px 20px 13px 20px;
    border-radius: 10px;
    background: #151414;

}
.close {
    background: #606061;
    color: #FFFFFF;
    line-height: 25px;
    position: absolute;
    right: -12px;
    text-align: center;
    top: -10px;
    width: 24px;
    text-decoration: none;
    font-weight: bold;
    -webkit-border-radius: 12px;
    -moz-border-radius: 12px;
    border-radius: 12px;
    -moz-box-shadow: 1px 1px 3px #000;
    -webkit-box-shadow: 1px 1px 3px #000;
    box-shadow: 1px 1px 3px #000;
}

.close:hover { background: #00d9ff; }
</style>
<script type="text/javascript">
function myFunction() {
    window.open("#openModal","_self");
}
</script>
</head>

<body>
<input type="text" onSelect="myFunction()">
<div id="openModal" class="modalDialog">
    <div>
        <a href="#close" title="Close" class="close">X</a>
        <br>
        <br>
        <br>
        <br>
        <br>
        <input type="button" value="Button 1">
        <input type="button" value="Button 2">
    </div>
</div>
</body>
</html>

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

相关问题 document.onmousemove / onmmouseup防止在IE输入或textarea中选择文本 - document.onmousemove / onmmouseup prevent selecting text in IE input or textarea 在提交时将文本输入注释textarea html / jquery - input text into a comment textarea html/jquery on submit HTML表单:在输入中显示文本(文本区域) - HTML forms: display a text in an input (textarea) 在HTML textArea中的每个文本后添加空格 - Adding a space after each text in a HTML textArea 选择html元素文本作为JavaScript的输入 - Selecting html elements text as input for JavaScript 在HTML表单的文本输入字段上进行选择后,无法使DatePicker工作 - Unable to get DatePicker work after selecting on a text input field of a HTML form 同一html函数中的可编辑文本和输入字段(Textarea +输入) - Editable text and input field in same html function (Textarea + input) 输入或文本区域中的 Html2Canvas 文本未捕获文本 - Html2Canvas Text in input or textarea not capturing text 制表时选择textarea文本 - Selecting textarea text when tabbing 如何在HTML文本区域中从用户输入文本并删除HTML标记,还可以在文本区域中搜索单词? - How to input text from user in HTML textarea and remove the HTML tags and also search through the textarea for a word?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM