混合式app开发积累100条

1.如何获取焦点?

//输入框
 
//使失去焦点
 if (this.$refs['inputName']){
          this.$refs['inputName'].blur()
        }
//使获取焦点
 if (this.$refs['inputName']){
          this.$refs['inputName'].focus()//获取焦点
        }

2.输入框内容不能包含特殊字符

//输入框
 
var containSpecial = /[~#^$@%&!?%, *]/gi
      if (! this.NewUserName || this.NewUserName === ''){
         this.modalToast('场景名称还没有输入哟~')
         return
        }else if (containSpecial.test(this.NewUserName)){
           this.modalToast('场景名称中不能包含特殊字符')
          return
        }

3.判断字符串字符数(中文占两个,英文占1个)

  //获取字符串长度(英文占1个字符,中文汉字占2个字符)
      getStringLength (str){
        var realLength = 0, len = str.length, charCode = -1
        for (var i = 0; i < len; i++) {
          charCode = str.charCodeAt(i)
          if (charCode >= 0 && charCode <= 128){
            realLength += 1
          } else{
            realLength += 2
          }
        }
        console.log('realLength:'+realLength)
        return realLength
      }

4.截取字符串长度(英文占1个字符,中文汉字占2个字符)

  //截取字符串长度(英文占1个字符,中文汉字占2个字符)
    cutstr (str, len){
      console.log('cutstr')
      var str_length = 0
      var str_len = 0
      var str_cut = ''
      str_len = str.length
      console.log('str_len',str_len)
      for (var i = 0; i < str_len; i++) {
          let a = str.charCodeAt(i)
          let b = str.charAt(i)
          if (a >= 0 && a <= 128) {
            str_length = str_length + 1
          }else {
            str_length = str_length + 2
          }
          console.log('str_length==',str_length)
          str_cut = str_cut + b
          if (str_length >= len) {
              str_cut = str_cut + '...'
              console.log('str_cut=',str_cut)
              return str_cut
          }
      }
      //如果给定字符串小于指定长度,则返回源字符串;  
      if (str_length < len) {
        console.log('str_cut=',str_cut)
          return str
      }
    }
//应用实例
 {{getStringLength(item.display_name) > 16 ? cutstr(item.display_name,16) : item.display_name}}

你可能感兴趣的:(混合式app开发积累100条)