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

后端 未结 5 1737
时光取名叫无心
时光取名叫无心 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 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:

    Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium?

    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.

提交回复
热议问题