为什么我的按钮与我的 onclick 事件不同步?

乔尔·埃彭

我在学习 javascript 时遇到了一个关于使用 onclick 事件的视频,(https://www.youtube.com/watch?v=XQEfWd1lh4Q&list=PL4cUxeGkcC9i9Ae2D9Ee1RvylH38dKuET&index=41&t=23s)。基本上,这家伙可以扩展以显示一张卡片它切断的部分内容。我能够跟上并且只是通过观看视频才有所了解。所以为了加深我的理解,我决定重新编码视频中显示的内容,而不参考它。

我有点得到了我想要的东西,但有一件小事让我感到沮丧。当我运行我的代码时,我得到了带有“显示更多”按钮的浓缩卡片(这很好),但是当我按下按钮时,卡片会展开但按钮仍然显示“显示更多”(这是它开始变坏),只有当我第三次按下按钮以压缩卡片时,按钮才会显示“Show Less”。

var contentDiv = document.querySelector('.content');
var button = document.querySelector('.button');

button.onclick = function(){
  if(contentDiv.className == 'content'){
    contentDiv.classList.add('appear');
    button.innerHTML = 'Show More';
  } else if(contentDiv.className == 'content appear'){
    contentDiv.classList.remove('appear');
    button.innerHTML = 'Show Less';
  }
};
*{
  margin: 0;
  padding: 0;
}

body{
  background: #a1a1a1;
}

.group{
  margin-top: 10%;
  display: flex;
  flex-flow: column nowrap;
  align-items: center;
}

.content{
  background: #dedede;
  border-radius: 10px;
  width: 250px;
  height: 300px;
  overflow: hidden;
  color: #2e2e2e;
  background: #e8e8e8;

  transition: all .5s;
}

.appear{
  height: 530px;
  color: #e8e8e8;
  background: #2e2e2e;
}

.content p{
  margin: 10px;
}

.button{
  margin: 10px;
  background: #f0ca62;
  padding: 5px;
  border-radius: 10px;
  cursor: pointer;
}
<!DOCTYPE html>
<html>
  <head>
    <title>Events</title>
    <link rel="stylesheet" href="./css/style.css">
  </head>
  <body>
    <div class="group">
      <div class="content">
        <p>width inflation scrape lamp continuation insistence proof pioneer trait
          vegetation dorm bring ball charter joy brick cabinet nest use economist
          width inflation scrape lamp continuation insistence proof pioneer trait
          vegetation dorm bring ball charter joy brick cabinet nest use economist
          width inflation scrape lamp continuation insistence proof pioneer trait
          vegetation dorm bring ball charter joy brick cabinet nest use economist
        </p>
        <p>width inflation scrape lamp continuation insistence proof pioneer trait
          vegetation dorm bring ball charter joy brick cabinet nest use economist
          width inflation scrape lamp continuation insistence proof pioneer trait
          vegetation dorm bring ball charter joy brick cabinet nest use economist
        </p>
        <p>width inflation scrape lamp continuation insistence proof pioneer trait
          vegetation dorm bring ball charter joy brick cabinet nest use economist
        </p>
      </div>

      <a class="button">Show More</a>
    </div>


    <script type="text/javascript" src="./js/script.js"></script>
  </body>
</html>

链接到 codepen 中的代码

拉斐尔·埃斯特拉达

似乎 show more 和 show less 按钮颠倒了,试试这个:

button.onclick = function(){
  if(contentDiv.className == 'content'){
    contentDiv.classList.add('appear');
    button.innerHTML = 'Show Less';
  } else if(contentDiv.className == 'content appear'){
    contentDiv.classList.remove('appear');
    button.innerHTML = 'Show More';
  }
};

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么不同步?

为什么角度不同步我的 var 绑定与 ngModel?

为什么输出与我预期的不同?

为什么正文中的按钮与我的 NavigationView 工具栏中的按钮的行为不同?

为什么StringBuffer的readObject不同步?

NTP不同步我的时间

为什么我的点图与我的 shapefile 方向不同?

为什么我的 StringBuffer 变量不同步。其中 StringBuilder 变量表现为同步

我不确定为什么我的 onclick 按钮没有与我在文本框中输入的内容相关联?

我的进度栏与我的倒数计时器不同步

为什么我在 JavaScript 中的 onclick 事件不起作用?

为什么我的onClick事件不起作用?

为什么我的onClick事件没有在React上触发?

为什么我的onclick事件不起作用?

Visual Studio调试器与我的代码不同步

为什么我用这个不同步的代码反复得到相同的结果?

为什么新的SkyDrive客户端不同步我的所有内容?

为什么Vue和我的DOM Textarea不同步(不是反应性问题)?

为什么我的 mp4 文件的音频不同步

为什么使用浮点变量的计算与我的想法不同?

为什么 Keras 的 MSE 与我计算的不同?

为什么Appcellerator Titanium的终端与我的OS终端不同?

为什么fastlane工作目录与我设置的目录不同

为什么这种方法不同步?

为什么php的$_COOKIE不同步读取cookies?

HashMap不同步,那么为什么并发修改异常

为什么rsync不同步该文件?

我的文本字段的.getText函数不同步

我的onclick事件被忽略