"使用jQuery完成点击按钮将索引为偶数的div背景色改为绿色"\n\n可以使用以下代码来使用jQuery完成这个任务:\n\nHTML代码:\nhtml\n<button id=\"changeColorBtn\">点击按钮</button>\n<div>第一个div</div>\n<div>第二个div</div>\n<div>第三个div</div>\n<div>第四个div</div>\n<div>第五个div</div>\n<div>第六个div</div>\n\n\njQuery代码:\njavascript\n$(document).ready(function(){\n $(\"#changeColorBtn\").click(function(){\n $(\"div:even\").css(\"background-color\", \"green\");\n });\n});\n\n\n这段代码首先在文档准备好后,给按钮绑定了一个点击事件。当按钮被点击时,使用$(\"div:even\")选取所有索引为偶数的div元素,并使用.css(\"background-color\", \"green\")将它们的背景色改为绿色。


原文地址: https://www.cveoy.top/t/topic/pK7V 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录