How to change color of the selected text dynamically on click of button?

后端 未结 5 1723
时光取名叫无心
时光取名叫无心 2020-12-18 07:20

I am using the following code to change the color of text but it is not working.. Can anyone help me with this? the soloution in javascript or jquery anything is fine..

相关标签:
5条回答
  • 2020-12-18 07:59

    document.getElementById("change_color").onclick = function() {
      // Get Selection
      sel = window.getSelection();
      if (sel.rangeCount && sel.getRangeAt) {
        range = sel.getRangeAt(0);
      }
      // Set design mode to on
      document.designMode = "on";
      if (range) {
        sel.removeAllRanges();
        sel.addRange(range);
      }
      // Colorize text
      document.execCommand("ForeColor", false, "red");
      // Set design mode to off
      document.designMode = "off";
    }
    <span id="content" contenteditable>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent sit amet odio eu magna mattis vehicula. Duis egestas fermentum leo. Nunc eget dapibus eros, id egestas magna. Fusce non arcu non quam laoreet porttitor non non dui. Ut elit nisl, facilisis id hendrerit et, maximus at nunc. Fusce at consequat massa. Curabitur fermentum odio risus, vel egestas ligula rhoncus id. Nam pulvinar mollis consectetur. Aenean dictum ut tellus id fringilla. Maecenas rutrum ultrices leo, sed tincidunt massa tempus ac. Suspendisse potenti. Aenean eu tempus nisl. 
    </span>
    <br/><br/>
    <button id="change_color">Change Selected Text Color</button>

    0 讨论(0)
  • 2020-12-18 08:01

    Try this

    mark up

    <p>
    I am using the following code to change the color of text but it is not working.. Can    anyone help me with this? the soloution in javascript or jquery anything is fine..
    </p>
    

    Script

    <script type="text/javascript" >
    
       $(document).ready(function(){
           $("p").on("mouseup" , function(){
              selectedtext = selectedText();
              var replceText = "<span style='background:#cccccc' >"+selectedtext+"</span>";
              var gethtmlText = $(this).text();
              var replcedtext = gethtmlText.replace(selectedtext ,  replceText);
             $(this).html(replcedtext)
           });
     });
    
    function selectedText(){
        if(document.getSelection){
          return document.getSelection();
        }
        else if(document.selection){
          return document.selection.createRange().text;
        }
    }
    
    </script>
    
    0 讨论(0)
  • 2020-12-18 08:05

    The following code works when you select a text or word, the color will change:

    <style>
    ::selection {
        color:blue;
        background:yellow;
        font-size:14px;
    }
    
    ::-moz-selection {
        color:blue;
        background:yellow;
        font-size:14px;
    }
    </style>
    
    0 讨论(0)
  • 2020-12-18 08:10

    Check DEMO here http://jsfiddle.net/yeyene/GYuBv/7/

    Select text, and click button to change selected text color.

    function selectHTML() {
        try {
            if (window.ActiveXObject) {
                var c = document.selection.createRange();
                return c.htmlText;
            }
    
            var nNd = document.createElement("span");
            var w = getSelection().getRangeAt(0);
            w.surroundContents(nNd);
            return nNd.innerHTML;
        } catch (e) {
            if (window.ActiveXObject) {
                return document.selection.createRange();
            } else {
                return getSelection();
            }
        }
    }
    
    $(function() {
        $('#changeColor').click( function() {
            var mytext = selectHTML();
            // you can modify any css style here...
            $('span').css({"color":"red"});
        });
    });
    
    0 讨论(0)
  • 2020-12-18 08:21

    I've created a pretty short fiddle that demonstrates the use of jQuery to change the color of a piece of text.

    HTML:

    <p id="paragraph">Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium?</p>
    <button id="colorChanger">This Button changes the text color of the paragraph.</button>
    

    CSS:

    #paragraph {
        color: green;
    }
    

    JavaScript:

    $('#colorChanger').click(function() {
        $('#paragraph').css('color','black');
    });
    

    The code above shows that with any text you can change the color using jQuery's css method. Additionally, I used #paragraph to access the paragraph; however, you can use nth-child through jQuery, you can cycle through the children of a container using a loop and checking for the right one then using the css method from jQuery. These are just a few of the ways to change the color of a piece of text.

    0 讨论(0)
提交回复
热议问题