如何更改textarea选定文本的颜色? [英] How to change color of selected text of textarea?

查看:615
本文介绍了如何更改textarea选定文本的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 textarea ,我想改变所选文字的颜色。
示例:

  var text =abcdefg; 

我想改变所选文字的颜色 efg



这是我的代码:

  var input = document .getElementById( '区域'); 
var value = input.value.substr(input.selectionStart,input.selectionEnd - input.selectionStart);
$('#abc')。find(value).attr('color',color);


解决方案

使用 :: selection 的文字被选中。您可以将CSS属性添加到选定的元素文本。


$ b

var textarea = document.querySelector(textarea); textarea.selectionStart = 12; textarea.selectionEnd = 23;

  ::  -  moz-selection {color:red;} :: selection {color:red; }  

< textarea>我的textarea突出显示文本< / textarea> ;


I have textarea and i want to change color of selected text. Example:

var text = "abcdefg";

I want to change color of selected text efg.

here is my code:

var input = document.getElementById('area');
var value = input.value.substr(input.selectionStart, input.selectionEnd - input.selectionStart);
$('#abc').find(value).attr('color',color);

解决方案

Use ::selection CSS selector that select part of text is seleced. You can add your CSS property to selected text of elements.

var textarea = document.querySelector("textarea");
textarea.selectionStart = 12;
textarea.selectionEnd = 23;

::-moz-selection { 
    color: red;
}
::selection {
    color: red;  
}

<textarea>My textarea highlighted text</textarea>

这篇关于如何更改textarea选定文本的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆