HTML DOM 样式 borderLeft 属性
htmldomweb developmentfront end technology更新于 2024/3/9 19:24:17
HTML DOM borderLeft 属性用于获取或设置元素左边框属性的简写。borderLeft 属性包含 border-left-width、border-left-style 和 border-left-color。
以下是语法 −
设置 borderImageWidth 属性 −
object.style.borderLeft = "width style color|initial|inherit"
以上属性解释如下 −
| 参数 | 描述 |
|---|---|
| width | 用于设置左边框宽度。 |
| style | 用于设置左边框样式。 |
| color | 用于设置左边框颜色。 |
| initial | 用于设置此属性设置为默认值。 |
| initial | 用于将此属性设置为默认值。 |
| inherit | 用于继承父级属性值。 |
让我们看一个 borderLeft 属性的示例 −
示例
<!DOCTYPE html>
<html>
<head>
<style>
#P1 {
border-left: 4px solid magenta;
font-size: 1.5rem;
}
</style>
<script>
function changeBorderLeft(){
document.getElementById("P1").style.borderLeft="9px dashed red";
document.getElementById("Sample").innerHTML="The left 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 left border properties by clicking the below button</p>
<button onclick="changeBorderLeft()">Change Border Left</button>
<p id="Sample"></p>
</body>
</html>
输出

点击"更改左边框"按钮 −

在上面的例子中 −
我们首先创建了一个 id 为"P1"的段落,其中包含一些文本,并应用了相应的 CSS 样式。
#P1 {
border-left: 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>
然后,我们创建了一个按钮 “更改左侧边框”,当用户点击该按钮时,它会执行 changeBorderLeft() 函数。
<button onclick="changeBorderLeft()">Change Border Left</button>
changeBorderLeft() 函数使用 getElementById() 方法获取 id 为 “P1” 的段落元素的 borderLeft 样式属性,并将其属性值更改为 ‘9px dashed red’ 。然后,id 为 “Sample” 的段落会使用其 innerHTML 属性显示一条消息,指示此更改。
function changeBorderLeft(){
document.getElementById("P1").style.borderLeft="9px dashed red";
document.getElementById("Sample").innerHTML="The left border for the paragraph element is now changed";
}
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

