如何使用 JavaScript 設定元素周圍輪廓的樣式?


要設定輪廓樣式,請使用 outlineStyle 屬性。輪廓可以是實線、點線、虛線等。

示例

你可以嘗試執行以下程式碼,使用 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 Style.</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 Style</button>
      <br>
      <script>
         function display() {
            document.getElementById("box").style.outlineColor = "#5F5F5F";
            document.getElementById("box").style.outlineStyle = "solid";
         }
      </script>
   </body>
</html>

更新於:2020 年 6 月 23 日

151 次檢視

開啟你的 職業生涯

透過完成該課程來獲得認證

開始
廣告
© . All rights reserved.