如何利用 JavaScript 設定元素周圍邊框的寬度?


如需設定邊框,請使用 outlineWidth 屬性。您可以嘗試執行以下程式碼,用 JavaScript 設定元素周圍邊框的寬度 −

示例

線上演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         #box {
            width: 450px;
            background-color: orange;
            border: 3px solid red;
            margin-left: 20px;
         }
      </style>
   </head>
   <body>
      <p>Click below to add Outline Width.</p>
      <div id="box">
         <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
         <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
         <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
      </div>
      <br>
      <button type="button" onclick="display()">Set Outline Width</button>
      <br>
      <script>
         function display() {
            document.getElementById("box").style.outlineColor = "#5F5F5F";
            document.getElementById("box").style.outlineStyle = "solid";
            document.getElementById("box").style.outlineWidth = "7px";
         }
      </script>
   </body>
</html>

更新於:2020-06-23

101 次瀏覽

開啟你的 事業生涯

完成課程,獲得認證

開始學習
廣告