konglingjun1224 2019-04-22 14:41 采纳率: 0%
浏览 886

前端ajax请求后拼接html,增加父级元素的class属性不成功

下面代码中
$(obj).parent().addClass("active"),测试的时候是正常的使用console.log($(obj).parent())来输出看到是有的,但是在页面上查看却并没有active这个class属性
如果使用
$("#wrapper ul").find("li").eq(pageNum+2).addClass("active"),在页面上是可以正常显示的,有哪位大神知道是怎么回事呢?


    $.ajax({
        url: ctx+"/aaa/abc",
        dataType: "JSON",
        type: "post",
        data: getData,
        success: function (o) {
            if(o.list!=null){
                pageSize=o.pageSize;
                pageNum=o.pageNum;
                var result=o.list;
                    for (let i = 0; i <result.length ; i++) {
                    $("#wrapper >table >tbody").append("<tr id="+result[i].houseId+"></tr>")
                    $("#"+result[i].houseId).append("<td>"+result[i].community+"</td>")
                    $("#"+result[i].houseId).append("<td>"+result[i].houseNum+"</td>")
                    $("#"+result[i].houseId).append("<td>"+result[i].address+"</td>")
                    $("#"+result[i].houseId).append("<td>"+result[i].houseOwner+"</td>")
                    $("#"+result[i].houseId).append("<td>"+result[i].ownerPhone+"</td>")
                    $("#"+result[i].houseId).append("<td>"+result[i].ownerCardType+"</td>")
                    $("#"+result[i].houseId).append("<td>"+result[i].ownerCardId+"</td>")
                    $("#"+result[i].houseId).append("<td>"+result[i].watermeter+"</td>")
                    $("#"+result[i].houseId).append("<td>"+result[i].gasmeter+"</td>")
                    if ($.trim(ctx)!="" || typeof (ctx)!="undefined" || ctx!=null) {
                        $("#" + result[i].houseId).append("<td>" +
                            "<a style='cursor:pointer' onclick='delHouse(" + result[i].houseId + ")'>删除</a>" +
                            " | " +
                            "<a style='cursor:pointer' onclick='editHouse(" +JSON.stringify(result[i]) + ")' data-toggle='modal' data-target='#myModal'>编辑</a>" +
                            "</td>");
                    } else {
                        $("#" + result[i].houseId).append("<td>" +
                            "<a style='cursor:pointer' onclick='delHouse(" + result[i].houseId + "," + ctx + ")'>删除</a>" +
                            " | " +
                            "<a style='cursor:pointer' onclick='editHouse(" + JSON.stringify(result[i]) + "," + ctx + ")' data-toggle='modal' data-target='#myModal'>编辑</a>" +
                            "</td>");
                    }

                }
                $("#wrapper").append('<nav  style="text-align:center"></nav>');
                $("#wrapper nav").append("<ul class='pagination'></ul>");
                $("#wrapper ul").append('<li><a href="#">&laquo;</a></li>');
                $("#wrapper ul").append('<li><a href="#">首页</a></li>');
                $("#wrapper ul").append('<li><a href="#" onclick="prePage(this)">上一页</a></li>');
                for (let i = o.navigatepageNums[0]; i <= o.navigateLastPage; i++) {
                    $("#wrapper ul").append('<li ><a href="#">'+i+'</a></li>');
                }
                $("#wrapper ul").append('<li><a href="#" onclick="nextPage(this)">下一页</a></li>');
                $("#wrapper ul").append('<li><a href="#">末页</a></li>');
                $("#wrapper ul").append('<li><a href="#">&raquo;</a></li>');
                let li=$("#wrapper ul").find("li").eq(3).children("a").text();
                if(li==o.pageNum){
                    $("#wrapper ul").find("li").eq(3).addClass("active")
                }else{
                    $(obj).parent().addClass("active")
                    //$("#wrapper ul").find("li").eq(pageNum+2).addClass("active")
                    console.log($(obj).parent())
                    console.log("pageNum:"+pageNum)
                    console.log($("#wrapper ul"))
                }
            }
        }
    })
  • 写回答

2条回答 默认 最新

  • TisonWomg 2019-04-22 15:29
    关注

    建议检查一下obj是否获取正常
    可以尝试使用js的方法来实现:

    <div>
                <ul>
                    <li><span onclick="fun(this)">1</span></li>
                    <li><span onclick="fun(this)">2</span></li>
                </ul>
            </div>
    
            <script>
                function fun(obj){
                    $(obj).parent().click(function(){
                        this.setAttribute("class","back_red")
                    });
                }
            </script>
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)