gpt4 book ai didi

javascript - 如何隐藏父div的子元素

转载 作者:行者123 更新时间:2023-12-02 22:11:58 25 4
gpt4 key购买 nike

您好,我正在尝试动态删除“您好,这是简短描述 1 ”(请参阅​​代码)行,在 Accordion 按钮(“section-1”、“section-2”、“section-3”)上单击。现在它正在被删除,但唯一的问题是它正在删除所有其他按钮文本,我希望它应该仅从我单击的按钮中删除。这是我的代码。

var acc = document.getElementsByClassName("accordion");
var i;
var short = document.getElementsByClassName('short-desc')

for (i = 0; i < acc.length; i++) {


acc[i].addEventListener("click", function () {


this.classList.toggle("active");
var panel = this.nextElementSibling;


if (panel.style.display === "block") {
panel.style.display = "none";

}

else {
panel.style.display = "block";
$(".short").closest('small').hide();

}
});
}
.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
}

.active, .accordion:hover {
background-color: #ccc;
}

.panel {
padding: 0 18px;
display: none;
background-color: white;
overflow: hidden;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<style>

</style>
</head>
<body>

<h2>Accordion</h2>

<button class="accordion">Section 1

<small class="short" style="color:red; display:block">Hello this is short description 1 </small>

</button>
<div class="panel">

<p >Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>

<button class="accordion">Section 2
<small class="short" style="color:red; display:block">Hello this is short description 2 </small>
<p id="short-desc2" style="display:none;">Hello this is short description </p>
</button>
<div class="panel">

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>

<button class="accordion">Section 3
<small class="short" style="color:red; display:block">Hello this is short description 3</small>
</button>
<div class="panel">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
</body>
</html>

最佳答案

您可以更改

$(".short").closest('small').hide();

this.querySelector('small').style.display = 'none';

   var acc = document.getElementsByClassName("accordion");
var i;
var short = document.getElementsByClassName('short-desc')

for (i = 0; i < acc.length; i++) {


acc[i].addEventListener("click", function () {


this.classList.toggle("active");
var panel = this.nextElementSibling;


if (panel.style.display === "block") {
panel.style.display = "none";

}

else {
panel.style.display = "block";
//console.log(this.querySelector('small'))
this.querySelector('small').style.display = 'none';

}
});
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<style>
.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
}

.active, .accordion:hover {
background-color: #ccc;
}

.panel {
padding: 0 18px;
display: none;
background-color: white;
overflow: hidden;
}
</style>
</head>
<body>

<h2>Accordion</h2>

<button class="accordion">Section 1

<small class="short" style="color:red; display:block">Hello this is short description 1 </small>

</button>
<div class="panel">

<p >Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>

<button class="accordion">Section 2
<small class="short" style="color:red; display:block">Hello this is short description 2 </small>
<p id="short-desc2" style="display:none;">Hello this is short description </p>
</button>
<div class="panel">

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>

<button class="accordion">Section 3
<small class="short" style="color:red; display:block">Hello this is short description 3</small>
</button>
<div class="panel">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>



</body>
</html>

关于javascript - 如何隐藏父div的子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59528339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com