监听ionic的生命周期事件解决dom还没有全部加载导致获取不了元素的bug

 ngAfterViewInit(): void {
    this.initViewAllBtn();
  }

查看全部按钮这个时候还没有加载所以需要通过声明周期钩子回调

 initViewAllBtn() {
    const description = document.getElementById('desc') as HTMLElement;
    if (description == null) {
      return;
    }
    const viewAllDiv = document.createElement("div");
    // const all = document.getElementById('all');
    const descString = description.textContent;
    const showString = '查看全部';
    const hideString = '收起';
    const n = description.offsetHeight;
    // if (n === 0) {
    //   return;
    // }
    viewAllDiv.style.fontSize = '1rem';
    viewAllDiv.style.color = '#637aa6';
    viewAllDiv.style.marginTop = '0.63rem';
    viewAllDiv.style.lineHeight = '1rem';
    if (n <= 110) {
      // all.style.display = 'none';
      viewAllDiv.style.display = 'none';
      // description.style.height = 'auto';
    } else {
      viewAllDiv.innerHTML = showString;
      viewAllDiv.style.display = 'block';
      // all.innerHTML = showString;
      // all.style.display = 'block';
      description.setAttribute('class', 'sub-show');
    }
    viewAllDiv.onclick = (e) => {
      if (description.classList.length > 0) {
        description.innerHTML = descString;
        description.classList.remove("sub-show");
        viewAllDiv.innerHTML = hideString;
      } else {
        description.innerHTML = descString;
        viewAllDiv.innerHTML = showString;
        description.setAttribute('class', 'sub-show');

      }
    };
    if (document.getElementById(this.descriptionSectionId)) {
      document.getElementById(this.descriptionSectionId).appendChild(viewAllDiv);
    }
  }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值