gpt4 book ai didi

javascript - 如何按类名在div周围放置边框

转载 作者:行者123 更新时间:2023-11-30 20:33:08 25 4
gpt4 key购买 nike

这是我的带有多个 div 的 html 文件,我希望在每个 div 元素中放置蓝色边框

<div class="myDiv">
<p><span style="color: #00bcf4;"><strong>1) <u>Units of Measurement</u></strong></span></p>
<p><strong><em>@ K</em></strong><em>ing &nbsp;<strong>H</strong>ector&nbsp; <strong>D</strong>ied &nbsp;<strong>M</strong>ysteriously&nbsp;<strong>D</strong>rinking&nbsp;</em></p>
<span style="color: #0000ff;"><strong>&nbsp; &nbsp;K</strong>ilo&nbsp;&nbsp;<strong>H</strong>ecto&nbsp; &nbsp;<strong>D</strong>eka &nbsp;<strong>M</strong>eter&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>D</strong>eci&nbsp;&nbsp;&nbsp;&nbsp;</span>
<p>&nbsp;</p>
<p><strong><em>C</em></strong><em>hoklate&nbsp;<strong>M</strong>ilk</em></p>
<span style="color: #0000ff;"><strong>C</strong>enti&nbsp;&nbsp;&nbsp;&nbsp; Milli</span>
<p>&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;</p>
</div>

<div class="myDiv">

<p><span style="color: #00bcf4;"><strong><u>2) Tens ' Multipliers</u></strong></span></p>
<p><strong><em>@ D</em></strong><em>esi&nbsp;</em>&nbsp; &nbsp;<strong><em>H</em></strong><em>ero</em>&nbsp;&nbsp;<strong><em>K</em></strong><em>illed&nbsp;<strong>M</strong>ega&nbsp;<strong>G</strong>igantic&nbsp;<strong>T</strong>errorists</em></p>
<span style="color: #0000ff;"><strong>&nbsp;&nbsp; D</strong>ekka&nbsp;&nbsp;<strong>H</strong>ecto &nbsp;<strong>K</strong>ilo&nbsp;&nbsp;<strong>M</strong>ega&nbsp;&nbsp;<strong>G</strong>iga&nbsp;&nbsp;&nbsp;&nbsp;<strong>T</strong>era</span>
<p>&nbsp; &nbsp;10<sup>1</sup>&nbsp;&nbsp;&nbsp;&nbsp;10<sup>2</sup>&nbsp;&nbsp;&nbsp;&nbsp;10<sup>3</sup>&nbsp; &nbsp;10<sup>6</sup>&nbsp;&nbsp;&nbsp;&nbsp;10<sup>9</sup>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;10<sup>12</sup></p>
<p>&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;&hellip;</p>
</div>

我试过了,但是没用

<script>
function myFunction() {
document.getElementByClassName("myDiv").style.border = "thick solid #0000FF";
}

</script>

<button type="button" onclick="myFunction()">Set border</button>

另外我想将功能设置为在页面加载时自动加载。

提前致谢。

最佳答案

在页面加载时运行您的函数非常简单:

<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function myFunction() {
document.getElementByClassName("myDiv").style.border = "thick solid #0000FF";
}
</script>
</head>
<body onload="myFunction();">

</body>
</html>

对于边框(使用 JavaScript):

document.getElementsById("myDiv").style.border = "thick solid #0000FF";

关于 getElementsById 的警告

关于javascript - 如何按类名在div周围放置边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50125713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com