文字溢位包含它的元素時,使用 JavaScript 要做什麼?
使用 textOverflow 屬性來處理文字溢位問題。如果文字溢位包含它的元素,就新增省略號。
示例
您可以嘗試執行以下程式碼,以瞭解文字溢位包含它的元素時使用 JavaScript 該怎麼做 −
<!DOCTYPE html>
<html>
<head>
<style>
#myID {
border: 2px solid blue;
background-color: gray;
overflow: hidden;
text-overflow: visible;
width: 200px;
white-space: nowrap;
}
</style>
</head>
<body>
<button onclick = "display()"> Click </button>
<div id = "myID">
This is Demo Text! This is Demo Text! This is Demo Text!<br>
This is Demo Text! This is Demo Text! This is Demo Text!<br>
This is Demo Text! This is Demo Text! This is Demo Text!<br>
This is Demo Text! This is Demo Text! This is Demo Text!<br>
</div>
<script>
function display() {
document.getElementById("myID").style.textOverflow = "ellipsis";
}
</script>
</body>
</html>
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP