javascript - 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>
解决方案
只需将错字改正document.getElementbyId
,document.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>
推荐阅读
- javascript - 使 JavaScript 画布背景透明
- unix - rsyslog 是否转发评论(#)?
- mysql - 为什么记录在 40 秒后到达
- excel - 有没有办法使用自动化将图片从内存加载到 excel 中?
- google-cloud-platform - GCP:无法连接到 Datalab
- python - 仅返回 x 变量方程之前的数字
- javascript - 如何在 react-native 中使用 node.js 和 Socket.io 实现实时聊天?
- c++ - C++ OpenCV卡尔曼滤波器构造函数错误
- grep - GREP:如何匹配正数或负数?
- python - 在python中使用带有多个and条件的when语句