1.如何獲取焦點(diǎn)?
//輸入框
<input ref="inputName" class="modifyNameInputContent" v-model="NewUserName" max-length="16" placeholder="請輸入新昵稱" autofocus="true"/>
//使失去焦點(diǎn)
if (this.$refs['inputName']){
this.$refs['inputName'].blur()
}
//使獲取焦點(diǎn)
if (this.$refs['inputName']){
this.$refs['inputName'].focus()//獲取焦點(diǎn)
}
2.輸入框內(nèi)容不能包含特殊字符
//輸入框
<input ref="inputName" class="modifyNameInputContent" v-model="NewUserName" max-length="16" placeholder="請輸入新昵稱" autofocus="true"/>
var containSpecial = /[~#^$@%&!?%, *]/gi
if (! this.NewUserName || this.NewUserName === ''){
this.modalToast('場景名稱還沒有輸入喲~')
return
}else if (containSpecial.test(this.NewUserName)){
this.modalToast('場景名稱中不能包含特殊字符')
return
}
3.判斷字符串字符數(shù)(中文占兩個,英文占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
}
}
//應(yīng)用實(shí)例
<text class="familyLabel">{{getStringLength(item.display_name) > 16 ? cutstr(item.display_name,16) : item.display_name}}</text>
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者