javascript - 为什么“阅读更多”按钮在 Div 中不起作用?
问题描述
我正在使用 divi 创建一个网站,并且我手动添加了一个不起作用的 READ MORE 按钮。请帮忙!
我使用了一个复杂的代码,我把它缩小到完全匹配 w3school 的,但它仍然不起作用。
function myFunction() {
var dots = document.getElementById("dots");
var moreText = document.getElementById("more");
var btnText = document.getElementById("myBtn");
if (dots.style.display === "none") {
dots.style.display = "inline";
btnText.innerHTML = "Read more";
moreText.style.display = "none";
} else {
dots.style.display = "none";
btnText.innerHTML = "Read less";
moreText.style.display = "inline";
}
}
#more {display: none;}
<meta name="viewport" content="width=device-width, initial-scale=1">
<h3><em>Let me tell you a Story!!!!!</em></h3>
<p style="text-align: left;">Let us imagine a boy. A brilliant, hardworking and dedicated chap. Always thinking about how he is going to study everything, what more he’s going to learn today and how one day he’s going to change the world. Everyone got inspired when they see this gentleman. One day a person finds out that this extraordinary lad is deciding to leave <span id="dots">...</span><span id="more">
the college, just like a sapling deciding to quit sunlight. He was appalled at the possibility that the world will never see that immense potential realized. He digs deeper and discovers the most unfortunate situation. The boy’s father was an addict, his brothers were hardly supportive and he was barely able to pay his fee till now. And now the support was gone. He couldn’t pay the pence that kept the ball rolling and the wheels of the train to his future turning. Imagine being denied of oxygen of wisdom for your lungs, glucose of rationality for your brain and choir of knowledge in your veins.
<br>
Now imagine as 22 million children in our country are denied even primary education. The person wanted to help him but he couldn’t possibly pay that much money. He took the name of Allah and started asking for donations. He believed in the humankind and its potential for good. It wasn’t easy at first but soon enough he could see the power of drops becoming waves and waves becoming an ocean. He witnessed the power of collective good, the emergence of something greater than the constituents. He was humbled by the grace of God that day. The boy’s fee was paid but the<br>burning flame of service still remained.
<br>
<em><strong>That flame was the birth of Muaawin and that person was I, Muhammad Shaheer Farooq and the boy became a friend, not to mention a top student.</strong></em></p>
<button onclick="myFunction()" id="myBtn">Read more</button>
解决方案
您插入的代码工作正常。
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#more {display: none;}
</style>
<h3><em>Let me tell you a Story!!!!!</em></h3>
<p style="text-align: left;">Let us imagine a boy. A brilliant, hardworking and dedicated chap. Always thinking about how he is going to study everything, what more he’s going to learn today and how one day he’s going to change the world. Everyone got inspired when they see this gentleman. One day a person finds out that this extraordinary lad is deciding to leave <span id="dots">...</span><span id="more">
the college, just like a sapling deciding to quit sunlight. He was appalled at the possibility that the world will never see that immense potential realized. He digs deeper and discovers the most unfortunate situation. The boy’s father was an addict, his brothers were hardly supportive and he was barely able to pay his fee till now. And now the support was gone. He couldn’t pay the pence that kept the ball rolling and the wheels of the train to his future turning. Imagine being denied of oxygen of wisdom for your lungs, glucose of rationality for your brain and choir of knowledge in your veins.
<br>
Now imagine as 22 million children in our country are denied even primary education. The person wanted to help him but he couldn’t possibly pay that much money. He took the name of Allah and started asking for donations. He believed in the humankind and its potential for good. It wasn’t easy at first but soon enough he could see the power of drops becoming waves and waves becoming an ocean. He witnessed the power of collective good, the emergence of something greater than the constituents. He was humbled by the grace of God that day. The boy’s fee was paid but the<br>burning flame of service still remained.
<br>
<em><strong>That flame was the birth of Muaawin and that person was I, Muhammad Shaheer Farooq and the boy became a friend, not to mention a top student.</strong></em></p>
<button onclick="myFunction()" id="myBtn">Read more</button>
<script>
function myFunction() {
var dots = document.getElementById("dots");
var moreText = document.getElementById("more");
var btnText = document.getElementById("myBtn");
if (dots.style.display === "none") {
dots.style.display = "inline";
btnText.innerHTML = "Read more";
moreText.style.display = "none";
} else {
dots.style.display = "none";
btnText.innerHTML = "Read less";
moreText.style.display = "inline";
}
}
</script>
推荐阅读
- python - 如何使用 MNIST 数据集实现超参数
- laravel - 在 scout:import Laravel 之后数字被格式化为字符串
- php - 我怎样才能让这个表格中的姓名、电子邮件和主题成为我从服务器收到的消息中的实际值?
- ios - 如果应用程序不在前台,DownloadTask 在作为后台获取的一部分执行时会暂停?
- python - 如何计算标量和向量之间的余弦相似度?
- random - 在表中定义键时,为什么按随机顺序插入?
- c# - 我可以使用单个 json 文件创建多个不同类的实例吗?
- intellij-idea - 添加选定的*句子*以供下次出现
- c++ - 在 Vulkan 中实现“实例化渲染”的正确方法是什么?
- android - 我应该使用什么样的服务在后台访问传感器数据?