HTML DOM 样式 borderTop 属性

htmlweb developmentfront end technology更新于 2024/3/10 21:00:17

HTML DOM borderTop 属性用于快速获取或设置元素的顶部边框属性。borderTop 属性包含 border-Top-width、border-Top-style 和 border-Top-color。

以下是语法 −

设置 borderTop 属性:

object.style.borderTop = "width style color|initial|inherit"

以上属性解释如下 −

参数描述
width用于设置顶部边框宽度。
style用于设置顶部边框样式。
color用于设置顶部边框颜色。
initial用于将此属性设置为默认值。
inherit用于继承父级属性值。

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

示例

<!DOCTYPE html>
<html>
<head>
<style>
   #P1 {
      border-Top: 4px solid magenta;
      font-size: 1.5rem;
   }
</style>
<script>
   function changeBorderTop(){
      document.getElementById("P1").style.borderTop="9px dashed red";
      document.getElementById("Sample").innerHTML="The Top border for the paragraph element is now  changed ";
   }
</script>
</head>
<body>
   <p id="P1">This is some sample text inside the paragraph. Here is another line of this sample text</p>
   <p>Change the above paragraph Top border properties by clicking the below button</p>
   <button onclick="changeBorderTop()">Change Border Top</button>
   <p id="Sample"></p>
</body>
</html>

输出

点击"更改顶部边框"按钮 −

在上面的例子中 −

我们首先创建了一个 id 为"P1"的段落,其中包含一些文本,并应用了相应的 CSS 样式。

#P1 {
   border-Top: 4px solid magenta;
   font-size: 1.5rem;
}
<p id="P1">This is some sample text inside the paragraph. Here is another line of this sample text</p>

然后我们创建了一个按钮"更改边框顶部",当用户点击该按钮时,它将执行changeBorderTop()函数。

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

changeBorderTop() 函数使用 getElementById() 方法获取 id 为 “P1” 的段落元素的 borderTop 样式属性,并将其属性值更改为 ‘9px dashed red’ 。然后,在 id 为 “Sample” 的段落中,使用其 innerHTML 属性显示一条指示此更改的消息。 −

function changeBorderTop(){
   document.getElementById("P1").style.borderTop="9px dashed red";
   document.getElementById("Sample").innerHTML="The top border for the paragraph element is now changed";
}

相关文章


有用资源