首页 > 解决方案 > Onclick 按钮功能展开文本框

问题描述

我正在按照教程创建一个展开和关闭文本框,但我似乎无法让 onclick 功能工作,当我单击按钮时它不会展开文本框。您是否看到任何会阻止 onclick 功能正常工作的错误?谢谢!

var content = document.getElementbyId("content");
var button = document.getElementById("show-more");

button.onclick = function() {
  if (content.className == "open") {
    //shrink the box
    content.className = "";
    button.innerHTML = "Show More";
  } else {
    //expand the box
    content.className = "open";
    button.innerHTML = "Show Less";
  }
};
<div id="content">
  <p>Lorem ipsum dolor sit amet etc.</p>
</div>

<a id="show-more">Show More</a>

标签: javascripthtmlbuttononclick

解决方案


只需将错字改正document.getElementbyIddocument.getElementById它应该可以工作。

在第一行寻找“ B ”。

var content = document.getElementById("content");
var button = document.getElementById("show-more");

button.onclick = function() {
  if (content.className == "open") {
    //shrink the box
    content.className = "";
    button.innerHTML = "Show More";
  } else {
    //expand the box
    content.className = "open";
    button.innerHTML = "Show Less";
  }
};
<div id="content">
  <p>Lorem ipsum dolor sit amet etc.</p>
</div>

<a id="show-more">Show More</a>


推荐阅读