如何使用 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.