jquery 判断文字是否超出div出现三个点的省略号

现在有个需求,就是一个div宽度固定,但是文字可能会超出,超出出现三个点省略,然后鼠标划入的时候显示全部,不超出鼠标划入就不显示,这就意味着要判断文字是否超出了

参考代码

1412138-20180627175947112-906318645.png

<html lang="en">
 <head>
  <title>Document</title>
 </head>
 <body>

    <div class="main-left">
        <span class="word fl">填报日期填报日期填报日期填报日期填报日期</span>
        <span>:</span>

        <span class="title">人填报人填报人填报人填报人</span>
    </div>
        <div class="main-right">
        <div class="layui-inline input-width-full">
            <input type="text" class="layui-input" placeholder="">
        </div>
    </div>

    <script>

        $(".main-left > .word").mouseenter(function (e) {
            var thisWidth = $(this).width(); // div 的宽度
            var wordWidth = $(this)[0].scrollWidth; // 先转为js对象; 文字的宽度
            if(wordWidth > thisWidth+5){ // 加5是为了让div宽度多一点,比文字不超出时多宽,因为文字不超出,那么宽度为div的宽度
                $(this).siblings('span.title').html($(this).text()).show();
            }
            
        })
        $(".main-left > .word").mouseleave(function () {
            // layer.msg('显示')
            $(this).siblings('span.title').hide();
        })
    </script>
  
 </body>
</html>

转载于:https://www.cnblogs.com/ybixian/p/9235305.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值