昨天我试着使用JavaScript让这段代码工作了几个小时,因为我想尽可能地掌握基础知识,但最终放弃了并用JQuery编写。希望能得到一些如何在纯JS中编写的建议。我只是试图使用一个按钮来显示隐藏的“div”,感觉应该很简单,只是我忽视了某些东西:
JQuery代码:
$('document').ready(function() {
$('.aboutBtn').click(function() {
$('#more-brian').toggleClass('hide');
});
})
HTML:
<div class="row">
<div class="card col-sm-6">
<h5 class="card-title">Brian Davis</h5>
<img class="card-img-top" src="../soccer-travel-site/img/brian-davis.jpg" alt="Brian Davis photo">
<div class="card-body">
<p class="card-text">Brian is the founder of AO Adventures and an original host of the BarrelProof Podcast.<button type="button" class="btn btn-danger aboutBtn">More About Brian</button></p>
</div>
<div id="more-brian" class="hide">
<p id="brian-para">Brian is a husband and father who is very passionate about soccer. He has been to over 50 U.S. Men's and Women's National Team games all around the world. In his spare time he is a co-host of the BarrelProof Podcast.</p>
</div>
</div>
</div>
我尝试了类似这样的东西:
function toggle(id){
var div1 = document.getElementById(id);
if (div1.style.display == 'none') {
div1.style.display = 'block'
} else {
div1.style.display = 'none'
}
}
我没有使用CSS的“hide”类。但它在页面加载时显示了div,然后通过按钮隐藏它,而我希望相反的情况发生。
hide
类,切换div1
的类列表。 - Andy