简体   繁体   English

如何更新点击的元素?

[英]How to update clicked element?

I am making a drawing board and I wish to get the color I click on.我正在制作一个绘图板,我希望得到我点击的颜色。 The problem: I only get the color I click on first.问题:我只得到我首先点击的颜色。 My idea: get the value by id and give it the appropriate color.我的想法:通过 id 获取值并给它适当的颜色。 Here is my code:这是我的代码:

<script>

            var colorNames = ["fuchsia","maroon", "red", "olive", "yellow", "green","lime", "teal", "aqua", "navy", "blue", "purple","black", "gray", "silver","white"];
            for (var i = 0; i < 16; i++) {
                document.write(`<div id="color${i}" class="color" style="background-color:${colorNames[i]}" onclick="draw(this)"></div>`);
            

            }
        console.log(colorNames)

        function draw(colorBox){

            var ChosenColorBox = colorBox.id;          
            console.log(ChosenColorBox)  
            var canvas = new fabric.Canvas("canvas");
           

            canvas.isDrawingMode = true;
            canvas.freeDrawingBrush.width = 5;
           
            
             if(ChosenColorBox=="color0"){
              canvas.freeDrawingBrush.color = "fuchsia";    
             }
            else if (ChosenColorBox=="color1")
            { 
               canvas.freeDrawingBrush.color = "maroon";
            }
    }
              
        




        </script>

You can get the color from element itself您可以从元素本身获取颜色

Example:例子:

 <script> var colorNames = ["fuchsia","maroon", "red", "olive", "yellow", "green","lime", "teal", "aqua", "navy", "blue", "purple","black", "gray", "silver","white"]; for (var i = 0; i < 16; i++) { document.write(`<div id="color${i}" class="color" style="background-color:${colorNames[i]}" onclick="draw(this)"></div>`); } console.log(colorNames) function draw(colorBox){ var ChosenColorBox = colorBox.id; console.log(ChosenColorBox) var canvas = new fabric.Canvas("canvas"); canvas.isDrawingMode = true; canvas.freeDrawingBrush.width = 5; canvas.freeDrawingBrush.color = colorBox.style['background-color'] } </script>

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

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