简体   繁体   English

单击交换按钮时,如何使红色div更改背景颜色以在红色和绿色之间切换?

[英]How do I get my red div to change background color to toggle between red and green when I click on the swap button?

In the following code, how do I get my red div to change background color to toggle between red and green when I click on the swap button? 在以下代码中,当我单击交换按钮时,如何使我的红色div更改背景颜色以在红色和绿色之间切换?

 $(document).ready(onReady); var numberOfClicks = 0; function onReady() { console.log('inside on ready'); $("#create").on('click', createNewDiv); // $(".color-div").on('click', '.delete', deleteDiv); } function createNewDiv() { console.log('inside createNewDiv'); var $div = $("<div class = 'color-div'>" + "<p>" + numberOfClicks++ +"</p>" + "</div>"); var $button1 = $('<button class = delete>Delete</button>'); var $button2 = $('<button class = swap>Swap</button>'); $('#container').append($div); $($div).append($button1); $($div).append($button2); //this is the event listener for the delete button $('.delete').on('click', function() { console.log('inside delete button'); $(this).parent().remove(); }); $('.swap').on('click', function() { console.log('inside swap button'); $(this).parent().toggleClass("green"); }); } function deleteDiv() { console.log('delete button pressed'); } 
 /* CSS Stylesheet */ /* ---------- DO NOT MODIFY THIS FILE ---------- */ body { font-family: sans-serif; } .color-div{ height:5em; width:100%; background-color: red; display: block; margin-bottom: 1em; } 
 <!DOCTYPE html> <!-- DO NOT MODIFY THIS FILE --> <html> <head> <meta charset="utf-8"> <title>First Code Challenge</title> <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> <script src="script.js" charset="utf-8"></script> <link rel="stylesheet" href="styles.css"> </head> <body> <header> <h1>My First Code Challenge</h1> </header> <main> <h1>Main Content Heading</h1> <button id="create">CREATE BUTTON</button> <div id="container"></div> </main> </body> </html> 

You're toggling a class "green" on click of swap, if you add a CSS class for green like so, it should work: 您要在单击交换时切换“绿色”类,如果您像这样为绿色添加CSS类,它应该可以工作:

 $(document).ready(onReady); var numberOfClicks = 0; function onReady() { console.log('inside on ready'); $("#create").on('click', createNewDiv); // $(".color-div").on('click', '.delete', deleteDiv); } function createNewDiv() { console.log('inside createNewDiv'); var $div = $("<div class = 'color-div'>" + "<p>" + numberOfClicks++ + "</p>" + "</div>"); var $button1 = $('<button class = delete>Delete</button>'); var $button2 = $('<button class = swap>Swap</button>'); $('#container').append($div); $($div).append($button1); $($div).append($button2); //this is the event listener for the delete button $('.delete').on('click', function() { console.log('inside delete button'); $(this).parent().remove(); }); $('.swap').on('click', function() { console.log('inside swap button'); $(this).parent().toggleClass("green"); }); } function deleteDiv() { console.log('delete button pressed'); } 
 /* CSS Stylesheet */ /* ---------- DO NOT MODIFY THIS FILE ---------- */ body { font-family: sans-serif; } .color-div { height: 5em; width: 100%; background-color: red; display: block; margin-bottom: 1em; } .green { background-color: green; } 
 <!DOCTYPE html> <!-- DO NOT MODIFY THIS FILE --> <html> <head> <meta charset="utf-8"> <title>First Code Challenge</title> <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> <script src="script.js" charset="utf-8"></script> <link rel="stylesheet" href="styles.css"> </head> <body> <header> <h1>My First Code Challenge</h1> </header> <main> <h1>Main Content Heading</h1> <button id="create">CREATE BUTTON</button> <div id="container"></div> </main> </body> </html> 

暂无
暂无

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

相关问题 如何让我的文本从红色 div 环绕到绿色 div? - How do I get my text to wrap from the red div to the green div? 如何更改我的 javascript 以便只有所选按钮为红色或绿色 - How do I change my javascript so that only the selected button is coloured red or green 如何使用表格中的单选按钮更改背景颜色。 每个单选按钮4种颜色(白色,红色,黄色,绿色) - How do i change background color using radio buttons in tables. 4 colors(white, red, yellow, green) to each radio buttons 每秒更改红色和绿色之间的背景颜色 - Change background color between red and green every second 我的标题的切换颜色在红色和绿色之间 - Toggeling Color of my header between red and green 如何将菜单上的Cufon字体的悬停颜色更改为红色? - How do I change the hover color to red for Cufon fonts on menu? 当我点击圆圈时,如何使用 Javascript 使圆圈改变颜色? 另外,如果所有圆圈都是红色,我该如何显示文字? 一世 - How do I make the circles change color with Javascript when I click on them? Also, how do I make text appear if all circles are red? I 我希望球撞到墙上时颜色变为红色。如何添加该代码? - I want the color of the ball to change the red when it hits the wall.How do I add that code? 如何使用 CSS 修复红色和绿色块? - How do I fix red and green blocks with CSS ? 如何在单击按钮时将 select 边框颜色更改为红色 - how to change select border color to red on button click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM