zoukankan      html  css  js  c++  java
  • 小程序操作DOM以及JS求取字符串算法(前端网备份)

     //js获取字符串的字节长度
    //这套算法一个汉字2字节,字母符号1字节,按一行40个字节算4行
      getLength:function(val){
        var str = new String(val);
        var bytesCount = 0;
        for (var i = 0, n = str.length; i < n; i++) {
          var c = str.charCodeAt(i);
          if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
            bytesCount += 1;
          } else {
            bytesCount += 2;
          }
        }
        return bytesCount;  
      },
      onReady: function () {
        var that =this;
        setTimeout(function () {
          //要延时执行的代码
          var query = wx.createSelectorQuery();
          query.select('.pContent').boundingClientRect()
          query.exec((res) => {
            console.log(res);
            var pContentHeight = res[0].height;
            console.log("ready", pContentHeight);
            if (pContentHeight < 82 || pContentHeight==87) {
              that.setData({
                hidden: true
              });
            }
            that.setData({
              pContentHeight: pContentHeight
            });
          })
        },300) 
    
      },

    初始化DOM求高度是有BUG的,只能第一次渲染后求,不加延迟有时候求不到值,加了延迟第一面效果不好

  • 相关阅读:
    Class attributes
    Card objects
    Exercises
    Type-base dispatch
    Operator overloading
    The str method
    loadrunner协议开发
    nmon分析与详解
    如何判断CPU、内存、磁盘的性能瓶颈?
    用友NC客户端地址
  • 原文地址:https://www.cnblogs.com/lsc-boke/p/10996825.html
Copyright © 2011-2022 走看看