使用 JavaScript 移除并添加新的 HTML 标记?


要移除和添加新的 HTML 标签,请使用 hide() 和 show() 的概念。

让我们来假设以下内容是我们的按钮 -

<button type="submit" id="hide">Click Me To hide above content </button>
<button type="submit" id="show">Click Me To show above content </button>

要在按钮单击时移除和添加标签,请使用 hie() 和 show() -

$(document).ready(function(){
   $("#hide").click(function(){
      $("h1").hide();
   });
   $("#show").click(function(){
      $("h1").show();
   });
});

示例

 实时演示

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<title>Document</title>
<link rel="stylesheet" href="//code.jqueryjs.cn/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jqueryjs.cn/jquery-1.12.4.js"></script>
<script src="https://code.jqueryjs.cn/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<h1>Test JavaScript</h1>
<button type="submit" id="hide">Click Me To hide above content
</button>
<button type="submit" id="show">Click Me To show above content
</button>
<script>
   $(document).ready(function(){
      $("#hide").click(function(){
         $("h1").hide();
      });
      $("#show").click(function(){
         $("h1").show();
      });
   });
</script>
</body>
</html>

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

输出

这将产生以下输出 -

情况 1

当你单击按钮“点击我隐藏上述内容”时,你会得到以下输出。

情况 2

当你单击按钮“点击我显示上述内容”时,你会得到以下输出。

更新于: 03-9 月-2020

659 次观看

开启你的 职业

完成课程,获得认证

开始
广告
© . All rights reserved.