js for循环 添加html代码

本文介绍了一种使用JavaScript实现的轮播图功能,包括动态生成轮播图组件并提供点击更换图片的功能。此外,还实现了通过AJAX请求更新轮播图的显示状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

###for循环代码 for(var i=0;i<res.data.length;i++){ var bannerPicList = res.data[i]; var bannerId = bannerPicList.id; var displayShow = bannerPicList.show; innerHTML+= "<div style='float:left;width:30%;margin:9px;position:relative;'>"+ "<img style='width:100%;height:150px;' src="+bannerPicList.bannerUrl+">"+ '<button class="btn btn-success" style="position:absolute;top:35%;left:38%;cursor:pointer" data-toggle="modal" data-target=".bs-example-modal-sm" οnclick="changePictureId(\'' + bannerId + '\')">点击更换</button>'+ "<div class='row'>"+ "<div id=displayBlockYes"+bannerId+">"+ "<label class='control-label col-md-4' style='margin-left:12px;'>"+"是否显示图片"+"</label>"+ "<div>";//结束开始做js判断 if(displayShow == true){ innerHTML+= '<input type="checkbox" οnchange="displayBlock(event,\'' + bannerId + '\',\'' + displayShow + '\')" checked>是'; }else{ innerHTML+= '<input type="checkbox" οnchange="displayBlock(event,\'' + bannerId + '\',\'' + displayShow + '\')">是'; } innerHTML+= "</div>"+ "</div>"+ "</div>"+ '</div>';

        }
        $("#bannerPicture").html(innerHTML);
  request请求
    //是否显示轮播图
    function displayBlock(event,id,displayShow){
        console.log(event);
        console.log(id);
        var show;
        if(displayShow == "true"){
            show = false;
           }else{

            show = true;
        }
    $.ajax({

        type:'POST',
        url:pengPengUrl+"/cmsComm/showBannerPic",
        dataType:"json",
        data:{bannerId:id,show:show},
        success:function(res){

            if(res.statusCode == 200){

                console.log(res);
                alert("修改成功");
                bannerPicList();
            }
        },
        error:function(){

            console.log("修改图片失败");
        }
    })

}

转载于:https://my.oschina.net/pingheyongfeng/blog/983550

HTML 页面中使用 `for` 循环通常需要结合 JavaScript 来实现,因为 HTML 本身是一种标记语言,不具备编程能力。JavaScript 可以在网页中动态生成内容,例如通过 `for` 循环来重复输出某些 HTML 元素[^4]。 以下是一个包含简单 `for` 循环HTML 页面示例代码,该循环会在页面上生成 10 个段落,每个段落显示一个数字: ```html <!DOCTYPE html> <html> <head> <title>For 循环示例</title> </head> <body> <h1>使用 JavaScriptfor 循环</h1> <div id="loop-output"></div> <script> for (let i = 1; i <= 10; i++) { document.getElementById("loop-output").innerHTML += "<p>这是第 " + i + " 个段落。</p>"; } </script> </body> </html> ``` 在该示例中,`for` 循环从 1 开始,到 10 结束,每次循环都会在 `<div>` 中添加一个新的 `<p>` 元素,并显示当前的数字 `i`。这种方式使得网页可以根据逻辑动态生成内容,而不是静态地写死内容。 ### 扩展应用 `for` 循环不仅可以用来生成静态内容,还可以用于遍历数组或对象,从而实现更复杂的数据展示。例如,遍历一个字符串数组并逐个输出元素: ```html <!DOCTYPE html> <html> <head> <title>遍历数组示例</title> </head> <body> <h1>遍历数组</h1> <ul id="list-output"></ul> <script> const fruits = ["苹果", "香蕉", "橙子", "葡萄", "西瓜"]; for (let i = 0; i < fruits.length; i++) { document.getElementById("list-output").innerHTML += "<li>" + fruits[i] + "</li>"; } </script> </body> </html> ``` 这段代码将数组中的每个水果名称作为一个列表项 `<li>` 添加页面中,形成一个无序列表。通过这种方式,可以轻松地将数据与 HTML 结构分离,提升页面的可维护性和扩展性[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值