Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

给文字设置了text-overflow:ellipsis

但我想要

  • 在文字显示...的时候,鼠标放上去会有tooltip显示完整字节
  • 完整显示的时候,鼠标放上去不显示tooltip

所以在写tooltip的显示状况必须得提前判断当前文字是否溢出

请问在vue里要如何判断文字是否溢出呢?

我一开始是估计的一个最大字符串长度值,但因为英文标点符合和中文的长度不一样,导致这个长度判断并不准确

所以有什么办法能精准判断文字是否溢出呢?(在文字可能包含标点英文中文的情况下)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
577 views
Welcome To Ask or Share your Answers For Others

1 Answer

computed: {
    lens: function () {
        var lens = 0; // 中文算2个字
        for (i = 0; i < this.message.length; i++) {
           if ((this.message.charCodeAt(i) >= 0) && (this.message.charCodeAt(i) <= 255))
               { lens = lens + 1;}
           else{ lens = lens + 2; } 
        }
        return lens;
    } 
},

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...