js判断元素内文字是否超出元素宽度,溢出隐藏
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>文字隐藏</title> </head> <style> .content-box { width: 100px; border: 5px solid red; padding: 10px; box-sizing: content-box; text-overflow: ellipsis; overflow: hidden; } .border-box { width: 100px; border: 5px solid red; padding: 10px; box-sizing: border-box; text-overflow: ellipsis; overflow: hidden; margin-bottom: 10px; } </style> <body> borderBox <div class="border-box"> 99999991 </div> contentBox <div class="content-box"> 999999999991 </div> <script> // 对于borderBox 只要scrollWidth > clientWidth 那么就代表hidden了 const borderBox = document.querySelector('.border-box') const contentBox = document.querySelector('.content-box') console.dir(borderBox) console.log('borderBox', borderBox.scrollWidth, borderBox.clientWidth) // zdz-log console.dir(contentBox) console.log('contentBox',contentBox.scrollWidth,contentBox.clientWidth); // zdz-log </script> </body> </html>
分类:
css/html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?