简体   繁体   English

在文本区域中显示突出显示的文本

[英]Displaying highlighted text in textarea

I have implemented a code that when i highlight a word or phrase it is added in the textarea but as soon as I click on the mouse the highlighted text value goes from the textarea. 我实现了一个代码,当我突出显示一个单词或短语时,它会添加到文本区域中,但是当我单击鼠标时,突出显示的文本值就会从文本区域中消失。

I am implementing this using Angularjs. 我正在使用Angularjs实现这一点。

This is my HTML & Angularjs code:


<div ng-app="myApp" ng-controller="caseCtrl">
<form name="aform" >
<textarea id="sel" rows="5" cols="20"></textarea>
</form>
 <div>
  <p>highlight the text to paste it in textbox.</p>
</div>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('caseCtrl', function($scope) {
 function getSelectionText() {
    var text = "";   
   if ((activeElTagName == "textarea")) {        
    } else if (window.getSelection) {
        text = window.getSelection().toString();
    }
  return text;
  }

  document.onclick = function() {
    document.getElementById("sel").value = getSelection();
       };
         });
    </script>

when i highlight the text it is pasted in textbox but when i click on the mouse the text gets disappeared from textbox. 当我突出显示文本时,它将粘贴在文本框中,但是当我单击鼠标时,文本将从文本框中消失。 Suggest me what to do ? 建议我怎么办?

In your case when you click somewhere, the selected text no longer exists, so text value gets updated to "" a blank string. 对于您而言,当您单击某个位置时,所选文本不再存在,因此text值将更新为""空白字符串。 You will have to put a check for that. 您将必须为此检查。

Your function should look like: Edited: 您的函数应如下所示

var text2 = "";
function getSelectionText() {
    var text = "";   
    if (window.getSelection) {
        text = window.getSelection().toString();
        console.log(text.length);
        if(text.length!=0) {
            text2 =text;
        }
    }
    return text2;
}

document.addEventListener("click", function() {
    document.getElementById("sel").value = getSelectionText();
});

如何访问当前突出显示的文本<textarea>&lt;i&gt;in Javascript&lt;/div&gt;&lt;/i&gt;&lt;b&gt;在Javascript中&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> 用户将在页面上的 HTML 文本区域中输入文本。 当他们突出显示该文本的一部分时,我如何访问突出显示的字符串? 突出显示 textarea 元素中的文本是否会触发任何事件?</p><p> 这是在文本区域是一个组件的反应应用程序的上下文中完成的。</p></div> - How to access the currently highlighted text within a <textarea> in Javascript

暂无
暂无

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

相关问题 在反应 js 中突出显示文本的 textarea 组件 - textarea component with highlighted text in react js 如何将突出显示的文本从textarea提交到数据库 - how to submit highlighted text from textarea into database 如何从textarea获取突出显示的文本位置? - How to get highlighted text position from textarea? Javascript打印文本区域,文本突出显示颜色 - Javascript print textarea with text highlighted colors 如何在另一个文本区域中显示文本区域和可编辑 div 中突出显示的文本的 position? - How to show the position of highlighted text in a textarea and editable div, in a another textarea? 如何访问当前突出显示的文本<textarea>&lt;i&gt;in Javascript&lt;/div&gt;&lt;/i&gt;&lt;b&gt;在Javascript中&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> 用户将在页面上的 HTML 文本区域中输入文本。 当他们突出显示该文本的一部分时,我如何访问突出显示的字符串? 突出显示 textarea 元素中的文本是否会触发任何事件?</p><p> 这是在文本区域是一个组件的反应应用程序的上下文中完成的。</p></div> - How to access the currently highlighted text within a <textarea> in Javascript 使用JQuery在textarea中获取光标位置和突出显示的文本 - Fetching cursor location and highlighted text in textarea using JQuery 从div获取突出显示的文本并复制到textarea javascript中 - Get highlighted text from div and copy into textarea javascript 如何从文本区域中突出显示的文本中获取邻居字符? - How to get neighbor character from highlighted text in the textarea? 使用javascript / jquery在文本区域内用强标签包装突出显示的文本 - Wrap highlighted text within textarea with strong tags using javascript/jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM