文字溢位包含它的元素時,使用 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>

更新於: 2020 年 6 月 23 日

65 次瀏覽

開啟你的職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.