繁体   English   中英

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

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

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

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

问题是,当您单击选择元素时,它会从文本区域窃取焦点。 您必须将焦点返回给textarea元素,这是一个有效的示例(至少在chrome中):

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

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

我一直在尝试更改文本区域内突出显示的内容的颜色,但是似乎您无法更改文本区域内的文本颜色。 如之前所建议的,另一种方法是创建一个可编辑的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(); };

暂无
暂无

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

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