Javascript 在按钮上显示特定元素单击

Javascript 在按钮上显示特定元素单击,javascript,jquery,html,Javascript,Jquery,Html,我遇到的情况是,用户会看到许多不同的按钮 单击任何按钮时,都会生成一个不同的表单,并在dom中注入一个字符串以通知用户按下了哪个表单 伪代码: <div id="notifier"> <h1 class="text-center"> <> SOMETHING SHOULD GO HERE<> <script> $(document).ready(function(){ $("#notif

我遇到的情况是,用户会看到许多不同的按钮

单击任何按钮时,都会生成一个不同的表单,并在dom中注入一个字符串以通知用户按下了哪个表单

伪代码:

<div id="notifier">
  <h1 class="text-center">
     <> SOMETHING SHOULD GO HERE<>

<script>
 $(document).ready(function(){
                $("#notifier").hide();  //hide the initial string since no form is pressed at the beginning
            $("#editForm1").hide(); //hides form1 since no button is pressed
            $("#editForm2").hide(); //hides form2 since no button is pressed


            $("#btn_1").click(function(e) {
                    //SOMETHING SHOULD GO HERE AS WELL
                    $("#editForm1").show(); 
按下按钮1时,将在dom中生成form1。 但是我还想在div中插入id=notifier的字符串Form1,让用户知道生成了哪个表单

我该怎么做

使用data-*属性存储目标选择器:data formshow=form-1 使用CSS类隐藏表单:.none{display:none;} 使用jQuery选择按钮和表单 使用jQuery.addClass和.removeClass操作.none类 使用。不从集合中排除特定元素: jQuery$=>{ const$notifier=$'notifier';//该 const$buttons=$'[data showform];//s const$forms=$'[id^=form-]';//s $buttons.点击按钮,功能EV{ 防止违约; const sel=$this.data'showform';//获取选择器 const txt=$this.text;//获取文本 const$form=$sel;//获取表单元素 $forms.not$form.addClass'hide';//隐藏其他表单 $form.removeClass'hide';//显示目标表单 $notifier.text txt;//将按钮文本预览到H1中 }; }; .hide{display:none;} 点击按钮 表格一 中二 我是中一 我是中二
jquery网站上充满了示例代码。我强烈建议您也进行探索和阅读