隐藏网页上的视频标签 - JavaScript
javascriptweb developmentfront end technologyobject oriented programming更新于 2026/2/14 13:32:17
假设我们在网页上有以下示例视频标签
<video class="hideVideo" width="350" height="255" controls> <source src="" id="unique_video_id"> 您无法在此处播放视频...... </video>
要隐藏网页上的视频,请使用 yourVariableName.style.display=’none’。
示例
以下是代码 −
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" />
<style>
.hideVideo {
display: block;
z-index: 999;
margin-top: 10px;
margin-left: 10px;
}
</style>
<body>
<video class="hideVideo" width="350" height="255" controls>
<source src="" id="unique_video_id">
您无法在此处播放视频......
</video>
</body>
<script>
var hideVideo = document.getElementsByClassName("hideVideo")[0];
hideVideo.style.display = "none";
</script>
</html>
要运行上述程序,请保存文件名"anyName.html(index.html)"。右键单击文件并选择选项"使用 Live Server 打开"在 Visual Studio Code 编辑器中。
输出
将在控制台上产生以下输出 −

在上面的示例输出中,视频标签已被禁用。如果您想启用,只需注释掉上面的行,即
//hideVideo.style.display = "none";
注释掉上面的行后,视频标签将启用,如下所示 −

相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

