jQuery中点击对象发生变化时,内容也随之变化

今天在做项目时遇到了这样的一个问题

 

当时我写的源代码是:

$(".nav-text").on("click", "div", function () {
  const word = $(this).text()
  $.ajax({
    url: "/irs/front/list",
    type: "post",
    data: JSON.stringify({
      code: "183bfb9e71e",
      searchFields: [
        {
          fieldName: "f_20221020157787",
          searchWord: "部门文件",
          withHighLight: true,
        },
        {
          fieldName: "f_20221010942026",
          searchWord: "",
          withHighLight: true,
        },
        {
          fieldName: "f_20221010711106",
          searchWord: "",
          withHighLight: true,
        },
        {
          fieldName: "f_20221010690856",
          searchWord: "",
          withHighLight: true,
        },
        {
          fieldName: "f_20221010433833",
          searchWord: "",
          withHighLight: true,
        },
        {
          fieldName: "f_20221020790441",
          searchWord: `${word}`,
          withHighLight: true,
        },
      ],
      tableName: "t_183bfb8fe36",
      pageNo: window.page,
      pageSize: "2",
      customFilter: {
        operator: "and",
        properties: [
          {
            property: "f_20221010627513",
            operator: "gte",           
            "value":"2022-08-26"
          },
          {
            property: "f_20221010627513",
            operator: "lte",           
            "value":"2022-09-26"
          },
        ],
      },
    }),
    headers: {
      "Content-Type": "application/json;charset=utf-8",
      athenaAppKey: rsaAppKey,
      athenaAppName: rsaAppName,
      transformRequest: [
        function (data) {
          return typeof data === "object" && String(data) !== "[object File]"
            ? toFormData(data)
            : data;
        },
      ],
    },
success: function (res) {

      const content = res.data.list

      console.log(content)

      content.map((item,index)=>{

        $('.order').append(`<div>${index+1}</div>`)

        $('#rq').append(`<div>${item.f_20221010627513.split(" ")[0]}`)

        $('.news').append(`<div>${item.f_20221010942026}</div>`)

      })

    }

 相信经验丰富的程序员一眼就看出来我的问题所在,正是因为我写的是append,append本身就是在后面进行追加元素,而不是我想的它会将第二次遍历完的替换掉第一次并追加在后面。

解决方法:找一个合适的时机将子元素内容全部清空。 

$(".nav-text").on("click", "div", function () {
  // 一上来先清空列表的子元素,防止后面无限追加
  $('.order,#rq,.news').empty()
  const word = $(this).text()
  $.ajax({
    url: "/irs/front/list",
    type: "post",
    data: JSON.stringify({
      code: "183bfb9e71e",
      searchFields: [
        {
          fieldName: "f_20221020157787",
          searchWord: "部门文件",
          withHighLight: true,
        },
        {
          fieldName: "f_20221010942026",
          searchWord: "",
          withHighLight: true,
        },
        {
          fieldName: "f_20221010711106",
          searchWord: "",
          withHighLight: true,
        },
        {
          fieldName: "f_20221010690856",
          searchWord: "",
          withHighLight: true,
        },
        {
          fieldName: "f_20221010433833",
          searchWord: "",
          withHighLight: true,
        },
        {
          fieldName: "f_20221020790441",
          searchWord: `${word}`,
          withHighLight: true,
        },
      ],
      tableName: "t_183bfb8fe36",
      pageNo: window.page,
      pageSize: "2",
      customFilter: {
        operator: "and",
        properties: [
          {
            property: "f_20221010627513",
            operator: "gte",
            "value":"2022-08-26"
          },
          {
            property: "f_20221010627513",
            operator: "lte",
            "value":"2022-09-26"
          },
        ],
      },
    }),
    headers: {
      "Content-Type": "application/json;charset=utf-8",
      athenaAppKey: rsaAppKey,
      athenaAppName: rsaAppName,
      transformRequest: [
        function (data) {
          return typeof data === "object" && String(data) !== "[object File]"
            ? toFormData(data)
            : data;
        },
      ],
    },
    success: function (res) {
      const content = res.data.list
      console.log(content)
      if(content.length === 0){
        $('.news').append("<p>暂无数据</p>")
      } else{
        content.map((item,index)=>{
          $('.order').append(`<div>${index+1}</div>`)
          $('#rq').append(`<div>${item.f_20221010627513.split(" ")[0]}`)
          $('.news').append(`<div>${item.f_20221010942026}</div>`)
        })
      }
    },
  });
});

最终效果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zwq8023520

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值