再次单击按钮时,如何将javascript效果恢复为显示:无?

Preet Kamal

<button onclick="updateProfile()">Change profile pic</button>
<form method='POST' id='form2' style="display:none;">
    {% csrf_token %}
    <fieldset class="form-group">
        <legend class="border-bottom mb-4">
            Join Today
        </legend>
        {{form2|crispy}}
    </fieldset>
</form>

   
function updateProfile() {
    console.log(this)
    document.getElementById("form1").style.display = "block";
}

我只能以一种方式更改效果。如何将效果改回显示:无,使表格不再可见?

米罗斯拉夫(Miroslav Glamuzina)

这样的事情将达到目的:

updateProfile = () => {
  let el = document.getElementById("form2");
  if (getComputedStyle(el).display === 'none') {
    el.style.display = "block";
  } else {
    el.style.display = "none";
  }
}
<button onclick="updateProfile()">Change profile pic</button>
<form method='POST' id='form2' style="display:none;">
  <fieldset class="form-group">
    <legend class="border-bottom mb-4">
      Join Today
    </legend>
  </fieldset>
</form>

或者,您可以利用以下data属性:

updateProfile = () => {
  let el = document.getElementById("form2");
  el.setAttribute('data-state',
    el.getAttribute('data-state') === 'open' ? 'closed' : 'open');

}
form[data-state=open] {
  visibility: visible;
}

form[data-state=closed] {
  visibility: hidden;
}
<button onclick="updateProfile()">Change profile pic</button>
<form method='POST' id='form2' data-state="closed">
  <fieldset class="form-group">
    <legend class="border-bottom mb-4">
      Join Today
    </legend>
  </fieldset>
</form>

甚至是一些简单的旧类:

updateProfile = () => {
  let el = document.getElementById("form2");
  if (new Set(el.classList).has('show')) {
    el.classList.remove('show');
    el.classList.add('hide');
  } else {
    el.classList.remove('hide');
    el.classList.add('show');
  }
}
.show {
  display: block;
}

.hide {
  display: none;
}
<button onclick="updateProfile()">Change profile pic</button>
<form method='POST' id='form2' class="show">
  <fieldset class="form-group">
    <legend class="border-bottom mb-4">
      Join Today
    </legend>
  </fieldset>
</form>

希望能有所帮助,

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在JQuery中再次单击时,如何将框的高度恢复为原始状态?

单击按钮时如何将文本设置为标签?

单击按钮时如何将selectedIndex设置为-1?

单击按钮时,ES6的div显示为无

在 PyQt4 (python) 中单击按钮时如何将文本字符串显示为标签?

如何将 json 数组中的项目显示为在按钮单击时唯一的反应本机模态

单击任何其他项目时如何将(最后单击的列表项)恢复为原始颜色-反应钩子

如何在单击时更改按钮状态并在 React 中再次单击时恢复到原始状态?

如何在单击时更改按钮的颜色,并在下次单击时恢复为默认颜色?

可拖动按钮时,如何将单击事件设置为禁用?

Android:单击时如何将onClickListener设置为其他按钮

撤消git pull,如何将存储库恢复为旧状态(再次)

如何使用bootstrap或jquery在单击按钮时将局部视图显示为弹出窗口/模式?

单击通知时如何将活动显示为弹出意图?

如何将ckeditor插件按钮显示为点击按钮

如何将navbarText设置为URL或可单击的按钮

单击按钮时如何将HTML文本框的值(内容)传递给javascript函数?

单击时Javascript按钮无响应

当按下按钮时,如何将HTML数组的数组显示为表格?

单击按钮时如何将url放入变量

单击按钮时如何将输入值设为空白?

当单击输入按钮时,php数组再次为空

单击按钮时,如何使下拉菜单恢复为默认选择

单击时我想显示按钮,再次单击时我想隐藏一个按钮

当一个按钮被包装成一个表单时,如何将按钮显示为内联/块元素?

如何将按钮的非悬停状态颜色覆盖为灰色,但是当悬停按钮显示颜色时(使用CSS框架)

再次单击按钮时出错

如何将选择选项显示为按钮?

如何将字典值数组显示为按钮?