如何改变 HTML 元素的可见度,背景色

改变背景颜色:

<!DOCTYPE html>
<html>
<head>

<script>
function bgChange(bg) {
  document.body.style.background = bg;
}
</script>
</head>
<body>

<h1>改变背景颜色</h1>

<p>请把鼠标移动到方块上。</p>

<table style="width:300px;height:100px">
  <tr>
    <td onmouseover="bgChange(this.style.backgroundColor)" 
    onmouseout="bgChange('transparent')"
    style="background-color:Khaki">
    </td>
    <td onmouseover="bgChange(this.style.backgroundColor)" 
    onmouseout="bgChange('transparent')"
    style="background-color:PaleGreen">
    </td>
    <td onmouseover="bgChange(this.style.backgroundColor)" 
    onmouseout="bgChange('transparent')"
    style="background-color:Silver">
    </td>
  </tr>
</table>

</body>
</html>

改变可见度:

<!DOCTYPE html>
<html>
<body>

<p id="p1">
这是一段文本。
这是一段文本。
这是一段文本。
</p>

<input type="button" value="隐藏文本" 
onclick="document.getElementById('p1').style.visibility='hidden'">

<input type="button" value="显示文本"
onclick="document.getElementById('p1').style.visibility='visible'">

</body>
</html>
发布了74 篇原创文章 · 获赞 27 · 访问量 9522

猜你喜欢

转载自blog.csdn.net/qq_42526440/article/details/100823645