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

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

最佳答案

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

关于javascript - 如果选择了文本并单击了下拉选项,则更改所选文本的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17729591/

相关文章:

javascript - 如何使用 webpack 2 正确打包 loadCSS 和 cssrelpreload?

JavaScript .closest 返回 null

html - 如何制作:hover height to not move object under it

html - CSS 将鼠标悬停在某物上并让完全不同的 div 更改其属性

jquery - 单击按钮显示打开对话框

php - 选择后下拉菜单不会填充

java - 是否有基于 Java SDK HTML 的文本区域插件

javascript - Node.js 不会在特定的 url 路径长度处加载 css

javascript - div被添加重复

HTML - 如何在我的 TEXTAREA 中禁用自动文本更正?