如何在 JavaScript 中允许长且不可拆分的单词被拆分并换行到下一行?

javascriptobject oriented programmingfront end technology更新于 2024/6/25 21:32:17

使用 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>

相关文章


有用资源