JavaScript设置div边框的步骤:

  • 获取div元素对象

  • 使用Style 对象的border属性来设置边框

border 属性在一个声明中设置所有边框属性。

语法:

Object.style.border=borderWidth borderStyle borderColor
描述
borderWidth 设置边框的宽度。
  • thin
  • medium
  • thick
  • length
borderStyle 设置边框的样式。
  • none
  • hidden
  • dotted
  • dashed
  • solid
  • double
  • groove
  • ridge
  • inset
  • outset
borderColor 设置边框的颜色。
  • color-name
  • color-rgb
  • color-hex
  • transparent

示例:

<!DOCTYPE html>
<html>
<head>
<style type="text/CSS">
div{ 
padding: 10px;
margin: 10px;
}
</style>
<script type="text/javascript">
function changeBorder()
{
document.getElementById("div1").style.border="thin dotted #FF0000";
}
</script>
</head>
<body>
<div id="div1">div元素,测试文本</div><br /><br />
<input type="button" onclick="changeBorder()" value="设置div边框" />
</body>
</html>

效果图:

javascript怎么设置div边框

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。