HTML DOM 样式 borderTopColor 属性

htmlweb developmentfront end technology更新于 2024/3/10 22:04:17

HTML DOM borderTopColor 属性用于获取或设置元素上边框的颜色。

以下是语法 −

设置 borderTopColor 属性 −

object.style.borderTopColor = "color|transparent|initial|inherit"

以上属性的解释如下 −

值描述
color用于指定上边框颜色。其默认颜色设置为黑色。
transparent使顶部边框颜色透明,可以看到下面的内容。
initial将此属性设置为默认值。
inherit继承父级属性值。

让我们看一个 borderTopColor 属性的示例 −

示例

<!DOCTYPE html>
<html>
<head>
<style>
   #IMG1 {
      border-top:solid 8px;
      border-top-color: orange;
   }
</style>
<script>
   function changeBorderTop(){
      document.getElementById("IMG1").style.borderTopColor="blue";
      document.getElementById("Sample").innerHTML="The border top color is now changed to blue";
   }
</script>
</head>
<body>
   <img id="IMG1" src="https://www.cainiaomax.com/blue_prism/images/blueprism-logo.jpg?auto=compress&cs=tinysrgb&dpr=2&h=550&w=150">
   <p>Change the above image border top color by clicking the below button</p>
   <button onclick="changeBorderTop()">Change Border Color</button>
   <p id="Sample"></p>
</body>
</html>

输出

点击"更改边框颜色"按钮时,−

在上面的例子中,−

我们创建了一个 ID 为"IMG1"的图像元素,并为其应用了 CSS 样式。该 CSS 样式指定了边框顶部的样式和颜色。

#IMG1 {
   border-top:solid 8px;
   border-top-color: orange;
}
<img id="IMG1" src="https://www.cainiaomax.com/blue_prism/images/blueprism-logo.jpg?auto=compress&cs=tinysrgb&dpr=2&h=550&w=150
">

然后我们创建了一个名为 “更改边框颜色” 的按钮,当用户点击该按钮时,会执行 changeBorderTop() 方法 −

<button onclick="changeBorderTop()">Change Border Color</button>

changeBorderTop() 方法获取 id 为 “IMG1” 的 <img> 元素使用 getElementById() 方法,并将其 borderTopColor 样式属性设置为蓝色。这会将顶部边框颜色更改为蓝色,并使用其 innerHTML 属性在 ID 为"Sample"的段落中显示此更改 −

function changeBorderTop(){
   document.getElementById("IMG1").style.borderTopColor="blue";
   document.getElementById("Sample").innerHTML="The border top color is now changed to blue";
}

相关文章


有用资源