简体   繁体   English

如果选择了文本,然后单击了下拉选项,则更改所选文本的颜色

[英]If text is selected, and dropdown option is clicked, change color of selected text

I want to make a text editor, and when you select some text in the text area, then click an option from the drop down, the selected text from the text area changes color. 我想创建一个文本编辑器,当您在文本区域中选择一些文本,然后从下拉菜单中单击一个选项时,从文本区域中选择的文本将更改颜色。 Unfortunately I don't know how to do this because when I try to access the drop down, the selection from the text area disappears. 不幸的是,我不知道该怎么做,因为当我尝试访问下拉菜单时,从文本区域中选择的内容就会消失。

jsFiddle :: http://jsfiddle.net/MatthewKosloski/a77sM/ jsFiddle :: http://jsfiddle.net/MatthewKosloski/a77sM/

function GetSelectedText () {
  if (window.getSelection) {  // all browsers, except IE before version 9
      var range = window.getSelection ();
      var selection = range.toString();
      alert(selection);
  } 
}   

The problem is when you click the select element it steals focus from textarea. 问题是,当您单击选择元素时,它会从文本区域窃取焦点。 You have to give back the focus to textarea element, here is a working example (at least in chrome): 您必须将焦点返回给textarea元素,这是一个有效的示例(至少在chrome中):

var color   = document.getElementById("color"), // this is the select element
    content = document.getElementById("content");

color.onfocus = function (){ content.focus(); };

I've been playing around with trying to change color of highlighted content within the textarea, but it seems that you cannot change the color of text within a textarea. 我一直在尝试更改文本区域内突出显示的内容的颜色,但是似乎您无法更改文本区域内的文本颜色。 An alternative, as has been suggested before, is to create an editable div that acts as a rich textbox. 如之前所建议的,另一种方法是创建一个可编辑的div,作为富文本框。 You can set the contentEditable property on the div to true in order for this to work. 您可以将div上的contentEditable属性设置为true,以使其起作用。 Here is my jsfiddle if you want to play around with it. 如果您想使用它,这是我的jsfiddle

And here is my JS code. 这是我的JS代码。 I changed a couple things on the HTML too so check out the jsfiddle for the full code. 我也更改了HTML上的几处内容,因此请查看jsfiddle以获取完整代码。

function GetSelectedText (origtext, seltext, tcolor) {
    //alert(origtext + ", " + seltext + ", " + tcolor);
    var divcontent = document.getElementById('sec');
    var spanTag = document.createElement("span");
    var selIndex = origtext.indexOf(seltext);
    var selLength = seltext.length;

    //split the text to insert a span with a new color
    var fpart = origtext.substr(0, selIndex);
    var spart = origtext.substr(selIndex + selLength);
    //alert(fpart + ", " + spart);

    // add the text that was highlighted and set the color
    spanTag.appendChild(document.createTextNode(seltext));
    spanTag.style.color = tcolor;

    //remove all the children of the div
    while(divcontent.hasChildNodes()){
         divcontent.removeChild(divcontent.lastChild);   
    }

    //append the original text with the highlighted part
    divcontent.appendChild(document.createTextNode(fpart));
    divcontent.appendChild(spanTag);
    divcontent.appendChild(document.createTextNode(spart));
} 

// this function was found at http://stackoverflow.com/questions/275761/how-to-get-selected-text-from-textbox-control-with-javascript

function getTextFieldSelection() {
    var textComponent = document.getElementById('content');
    var selectElem = document.getElementById("myselect");

    var selectedText;
    // IE version
    if (document.selection != undefined)
    {
         textComponent.focus();
         var sel = document.selection.createRange();
         selectedText = sel.text;
    }
    // Mozilla version
    else if (textComponent.selectionStart != undefined)
    {
         var startPos = textComponent.selectionStart;
         var endPos = textComponent.selectionEnd;
         selectedText = textComponent.value.substring(startPos, endPos)
    }
    //alert("You selected: " + selectedText);
    selectElem.onchange = GetSelectedText(textComponent.value, selectedText,selectElem.options[selectElem.selectedIndex].text.toLowerCase());
}

var content = document.getElementById("content");
var selectElem = document.getElementById("myselect");

selectElem.onfocus = function (e) { getTextFieldSelection(); };

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM