js实现单行溢出省略号…
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>js实现单行溢出省略号…</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </head> <body> <div id="ellipsis"> 浏览器兼容伪类写法css溢出部分css溢出部分css溢出部分css溢出部分css溢出部分css溢出部分css溢出部分 </div> <script type="text/javascript"> //参数1是允许最大文字数目,参数2是元素节点的#id或.class function mitulineHide(num,con){ var contain = $(con); var maxSize = num; var txt = $(contain).html(); if(txt.length>num){ txt = txt.substring(0,num-1)+"..."; $(contain).html(txt); }else{ console.log("error") } }; mitulineHide(30,'#ellipsis'); </script> </body> </html>