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

时间:2013-07-18 16:54:24

标签: javascript css textarea selection

我想创建一个文本编辑器,当您在文本区域中选择一些文本,然后从下拉列表中单击一个选项时,文本区域中的所选文本会更改颜色。不幸的是我不知道怎么做,因为当我尝试访问下拉列表时,文本区域的选择消失了。

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);
  } 
}   

2 个答案:

答案 0 :(得分:0)

问题是当你点击select元素时它会从textarea中窃取焦点。 你必须将焦点交还给textarea元素,这是一个工作示例(至少在chrome中):

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

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

答案 1 :(得分:0)

我一直在尝试改变textarea中突出显示内容的颜色,但似乎你无法改变textarea中文本的颜色。如前所述,另一种方法是创建一个可编辑的div,它充当富文本框。您可以将div上的contentEditable属性设置为true,以使其正常工作。如果你想玩它,这是我的jsfiddle

这是我的JS代码。我也在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(); };