【问题标题】:hide and show two buttons after clicked on it单击后隐藏和显示两个按钮
【发布时间】:2015-11-28 13:29:25
【问题描述】:

请在下面不使用onclick功能的情况下根据我的按钮给我一个示例:

<button id="button1">Example 1</button>
<button id="button2" style='display:none;'>Example 2</button>

例如:

Button1是默认的,点击后会隐藏,button2会隐藏 出现,再次点击 button2 后将隐藏并且 button1 将 出现。

我需要一个带有 ID 的 jQuery,而不是 onclick 函数。

是否可以获取隐藏和显示按钮的 ID?

提前致谢

【问题讨论】:

  • 您可以通过 Jquery 使用 $("#button1") 和 $("#button2") 获取 ID,但您需要在 click 函数中操作才能显示或隐藏
  • jquery 使用事件例如onclick,onhover,onkeypress,没有事件必须间隔自动化。

标签: jquery show-hide buttonclick


【解决方案1】:

你可以使用 not() 做一些简单的事情,如果没有点击事件处理程序你也做不到

var btn = $('#button1,#button2').click(function() { // bind click handler to both button
  $(this).hide(); // hide the clicked button
  btn.not(this).show(); // show the another button which is hidden
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="button1">Example 1</button>
<button id="button2" style='display:none;'>Example 2</button>

【讨论】:

  • 对不起,您的代码在这里运行良好,但是当我将其放入 html 时,它就无法运行,为什么?
  • @AlexanderRichard :在您的代码 &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt; 之前添加 jquery 库,并将您的代码放入文档就绪处理程序 $(document).ready(function(){ // code here });
  • @AlexanderRichard : 很高兴帮助你:)
猜你喜欢
  • 2020-05-21
  • 1970-01-01
  • 2016-03-02
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-21
  • 1970-01-01
相关资源
最近更新 更多