jquery foreach在响应式网页中用法

avatar
作者
猴君
阅读量:0

在响应式网页中使用jQuery的each()方法来遍历元素并进行相应的操作。以下是一个示例:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Responsive Website</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <style>     .box {         width: 100px;         height: 100px;         background-color: red;         margin: 10px;         float: left;     } </style> </head> <body> <div class="container">     <div class="box">Box 1</div>     <div class="box">Box 2</div>     <div class="box">Box 3</div>     <div class="box">Box 4</div> </div> <script>     $(document).ready(function() {         $(".box").each(function(index) {             $(this).css("background-color", "blue");         });     }); </script> </body> </html> 

在这个例子中,我们使用了jQuery的each()方法来遍历所有具有class为"box"的元素,并将它们的背景颜色改为蓝色。这样可以很方便地实现在不同尺寸的屏幕上更改元素样式的效果。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!