<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 500px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
</style>
</head>
<body>
<div class="box">JS判断元素是否出现了省略号,即文本内容的宽度是否超过了元素的宽度</div>
<script>
function main() {
let box = document.querySelector('.box')
console.log("scrollWidth: ", box.scrollWidth)
console.log("offsetWidth: ", box.offsetWidth)
if (box.scrollWidth > box.offsetWidth) {
console.log("出现了省略号")
} else {
console.log("没有出现省略号")
}
}
main()
</script>
</body>
</html>
转载于:https://blog.csdn.net/idaine/article/details/110958536