gpt4 book ai didi

javascript - 单击按钮时显示隐藏备用 div

转载 作者:行者123 更新时间:2023-11-28 19:06:46 25 4
gpt4 key购买 nike

嗨,我正在尝试实现一个 js/jquery 程序,其中步骤如下当有人单击按钮 1 时,首先隐藏包装 div,然后显示 2 按钮(第二个)的包装 div。第三次和第四次类似

<div class="wrapper">
<div class="first">
<div class="content">content1</div>
<button class="button">1</button>
</div>
<div class="second">
<div class="content">content2</div>
<button class="button">2</button>
</div>
</div>

<div class="wrapper">
<div class="first">
<div class="content">content1</div>
<button class="button">3</button>
</div>
<div class="second">
<div class="content">content2</div>
<button class="button">4</button>
</div>
</div>

我已经尝试过,但知道它不起作用

<script type="text/javascript">
$('.button').on('click', function(e){
console.log($( this));
$( this ).closest(".wrapper .second").hide();
});
</script>

最佳答案

你可以切换一个类:

$('.button').on('click', function() {
$(this).closest('div').add($(this).closest('.wrapper').find('.hidden')).toggleClass('hidden');
});
.hidden {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="wrapper">
<div class="first">
<div class="content">content1</div>
<button class="button">1</button>
</div>
<div class="second hidden">
<div class="content">content2</div>
<button class="button">2</button>
</div>
</div>
<div class="wrapper">
<div class="first">
<div class="content">content1</div>
<button class="button">3</button>
</div>
<div class="second hidden">
<div class="content">content2</div>
<button class="button">4</button>
</div>
</div>

JS:

$('.button').on('click', function () {
$(this).closest('div').add($(this).closest('.wrapper').find('.hidden')).toggleClass('hidden');
});

CSS:

.hidden {
display: none;
}

-jsFiddle-

关于javascript - 单击按钮时显示隐藏备用 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31523079/

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