在网页上隐藏视频标签 - JavaScript

假设我们在网页上有以下示例视频标签

<video class="hideVideo" width="350" height="255" controls>
   <source xx_src="" id="unique_video_id">
   You cannot play video here......
</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 xx_src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script xx_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 xx_src="" id="unique_video_id">
      You cannot play video here......
   </video>
</body>
<script>
   var hideVideo = document.getElementsByClassName("hideVideo")[0];
   hideVideo.style.display = "none";
</script>
</html>

要运行上述程序,请保存文件名“anyName.html(index.html)”。右键单击该文件,然后在 Visual Studio Code 编辑器中选择“使用 Live Server 打开”选项。

输出结果

这将在控制台上产生以下输出 -

在上面的示例输出中,视频标签已被禁用。如果要启用,只需注释上面的行,即

//hideVideo.style.display = "none";

评论上述行后,视频标签将被启用,如下所示 -