JQuery next()方法 click方法

tech2022-12-12  106

JQuery next()方法 click方法

改变当前元素的下一个元素的css 实例:点击按钮改变按钮的背景颜色同时改变div的背景颜色

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>document</title> <style> #box{ width: 200px; height: 200px; border: 1px solid black; } </style> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <button id="btn">点击</button> <div id="box">123</div> <script> $('#box').css({ "color":"red","font-size":"20px","width":"200px","height":"200px", "text-align":"center","line-height":"200px" }); $(function(){ $("#btn").click(function(){ $("#btn").css('background-color','red') .next() .css("background-color","black"); }) }) </script> </body> </html>
最新回复(0)