如何在 JavaScript 中允許對長且不可拆分的單詞進行拆分並換行到下一行?


在 JavaScript 中使用wordWrap 屬性可以允許對長單詞進行拆分並換行到下一行。

示例

你可以嘗試執行以下程式碼來學習如何使用wordWrap 屬性 −

<!DOCTYPE html>
<html>
   <head>
      <style>
         #box {
            width: 150px;
            height: 150px;
            background-color: lightblue;
            border: 1px solid black;
         }
      </style>
   </head>
   <body>
      <button onclick = "display()">Set</button>

      <div id = "box">
         ThisisDemoText.ThisisDemoText.ThisisDemoText.ThisisDemoText.Thisis DemoText.ThisisDemoText.
      </div>

      <script>
         function display() {
            document.getElementById("box").style.wordWrap = "break-word";
         }
      </script>
   </body>
</html>

更新日期:2020 年 6 月 23 日

297 次閱讀

開啟您的 職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.