Changing text color onclick

后端 未结 3 1344
一生所求
一生所求 2021-01-12 07:33

I\'m new to JavaScript. I\'ve developed a page using JavaScript in such a way that when I select a color it is applied to the whole page as a background.

I want to

相关标签:
3条回答
  • 2021-01-12 07:53

    A rewrite of the answer by Sarfraz would be something like this, I think:

    <script>
    
        document.getElementById('change').onclick = changeColor;   
    
        function changeColor() {
            document.body.style.color = "purple";
            return false;
        }   
    
    </script>
    

    You'd either have to put this script at the bottom of your page, right before the closing body tag, or put the handler assignment in a function called onload - or if you're using jQuery there's the very elegant $(document).ready(function() { ... } );

    Note that when you assign event handlers this way, it takes the functionality out of your HTML. Also note you set it equal to the function name -- no (). If you did onclick = myFunc(); the function would actually execute when the handler is being set.

    And I'm curious -- you knew enough to script changing the background color, but not the text color? strange:)

    0 讨论(0)
  • 2021-01-12 08:06
       <p id="text" onclick="func()">
        Click on text to change
    </p>
    <script>
    function func()
    {
        document.getElementById("text").style.color="red";
        document.getElementById("text").style.font="calibri";
    }
    </script>
    
    0 讨论(0)
  • 2021-01-12 08:07

    Do something like this:

    <script>
    function changeColor(id)
    {
      document.getElementById(id).style.color = "#ff0000"; // forecolor
      document.getElementById(id).style.backgroundColor = "#ff0000"; // backcolor
    }
    </script>
    
    <div id="myid">Hello There !!</div>
    
    <a href="#" onclick="changeColor('myid'); return false;">Change Color</a>
    
    0 讨论(0)
提交回复
热议问题