javascript - 基于按钮的div显示

标签 javascript jquery

我在下面有一个 jsfiddle:

http://jsfiddle.net/Jsh2V/10/

第一组测试按钮(“显示”和“隐藏”)效果很好。我一直在尝试让第二组按钮(“拥有”和“需要”)用 div 做同样的事情。不工作。有什么建议么?另外,如果有更有效的方法来做到这一点......

这里是 fiddle 的代码:

<!DOCTYPE html>
<html>
<head>
  <style>
      p { background:yellow; }
      </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <button class="1">Show it</button>
  <button class="2">Hide it</button>

      <p style="display: none" class="1">Hello</p>
      <p style="display: none" class="2">Un Hello!</p>
<script>
    $("button.1").click(function () {
    $("p.1").show("slow");
    $("p.2").hide("fast");
    });
</script>
<script>
    $("button.2").click(function () {
    $("p.1").hide("fast");
    $("p.2").show("slow");
    });
</script>


    <button class="have">Have</button>
    <button class="need">Need</button>

    <div id="have" style="display: none">HAVE YO</div>
    <div id="need" style="display: none">NEED YO</div>
<script>
    $("button.have").click(function () {
    $("div.have").show("slow");
    $("div.need").hide("fast");
    });
</script>
<script>
    $("button.need").click(function () {
    $("div.need").show("slow");
    $("div.have").hide("fast");
    });
</script>
</body>
</html>

最佳答案

应该是 div#have 而不是 div.have,因为你的 div 有 have 的 id,而不是 的类>有

<script>
    $("button.have").click(function () {
    $("div#have").show("slow");
    $("div#need").hide("fast");
    });
</script>
<script>
    $("button.need").click(function () {
    $("div#need").show("slow");
    $("div#have").hide("fast");
    });
</script>

关于javascript - 基于按钮的div显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8190958/

相关文章:

jquery - 在无逻辑 mustache 模板引擎中实现条件

jquery - .on() 绑定(bind)在触发后不起作用 ('create' )

javascript - 在 Kendo UI 中将网格 pageSize 设置为变量

javascript - "addEventListener is not a function"问题

javascript - 在 Ionic 应用程序上使用 $.getScript 加载 Google Map Api

javascript - Safari 浏览器 keydown 事件不起作用

javascript - 如何设置一个间隔并同时增加一个值?

javascript - 在 WebView 中打开一个弹出窗口

javascript - 自定义数组排序顺序 Javascript 的问题

php - 如何使用jquery将数据发送到另一台服务器