javascript - 按下按钮时出现 div

标签 javascript jquery html css

我无法使选定的 div 在按下按钮时出现和消失。

<button id="showButton" type="button">Show More</button>

<div id="container">
    <div id="fourthArticle">
        <img class="row1pic" id="pic4" src="../Pictures/Gamerati/00-Shub-Niggurath-Rampage.jpg" alt="Freud everyone!">
       <p>
       <span style="font-weight:bold;">This is a third article</span>
       this is the content of the article. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
       </p>
    </div>

    <div id="fifthArticle">
        <img class="row1pic" id="pic5" src="../Pictures/Gamerati/00-Shub-Niggurath-Rampage.jpg" alt="Freud everyone!">
       <p>
       <span style="font-weight:bold;">This is a third article</span>   
       this is the content of the article. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
       </p>
    </div>

    <div id="sixthArticle">
        <img class="row1pic" id="pic6" src="../Pictures/Gamerati/00-Shub-Niggurath-Rampage.jpg" alt="Freud everyone!">
       <p>
       <span style="font-weight:bold;">This is a third article</span> 
       this is the content of the article. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
       </p>
    </div>


<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">

$('#showButton').click(function () {
    $('#container').toggleClass('hidden');  
}

</script>

当我按下按钮时,没有任何反应。我尝试在 .click 函数中放置一个 alert,但也失败了。所以我认为这有问题,但据我所知,我不知道我可能做错了什么。

最佳答案

  1. 您的容器 div 未使用结束标记正确关闭 </div> .
  2. 您的事件处理程序未正确关闭。

$(function() {
  $('#showButton').click(function() {
    $('#container').toggleClass("hidden");
  });
});
.hidden{
  display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="showButton" type="button">Show More</button>

<div id="container">
  <div id="fourthArticle">
    <img class="row1pic" id="pic4" src="../Pictures/Gamerati/00-Shub-Niggurath-Rampage.jpg" alt="Freud everyone!">
    <p>
      <span style="font-weight:bold;">This is a third article</span>
      this is the content of the article. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
    </p>
  </div>

  <div id="fifthArticle">
    <img class="row1pic" id="pic5" src="../Pictures/Gamerati/00-Shub-Niggurath-Rampage.jpg" alt="Freud everyone!">
    <p>
      <span style="font-weight:bold;">This is a third article</span> this is the content of the article. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
    </p>
  </div>

  <div id="sixthArticle">
    <img class="row1pic" id="pic6" src="../Pictures/Gamerati/00-Shub-Niggurath-Rampage.jpg" alt="Freud everyone!">
    <p>
      <span style="font-weight:bold;">This is a third article</span> this is the content of the article. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
    </p>
  </div>
</div>

关于javascript - 按下按钮时出现 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34498307/

相关文章:

javascript - 当用户使用不同帐户打开新选项卡时,强制刷新旧选项卡中的站点

javascript - 如何通过现有数组标签明智地收集/制作新数组?

html - ie6边距扩大20px

javascript - 如何将 "Different"多个 <Button> 函数转换为带有 <Select> 的通用函数?

javascript - 语法帮助 - JavaScript 中的 HTML

javascript - 使用 jQuery/JavaScript 在动态输入文件字段中获取选定的文件名

javascript - 将配置页面设置为已保存在 Pebble 3.0 SDK 中的特定默认值

javascript - 为什么我的 iframe 的 contentEditable 会被重置?

javascript - 使用 jQuery,如何将 "scroll counter"添加到我的页面?

javascript - 检测具有无值属性的元素