英文:
How to toggle multiple divs using jQuery
问题
我有几个div
,应该在相应的按钮被点击时显示/隐藏。在初始状态下,所有按钮和所有div
都是可见的。当点击第一个按钮时,只有相应的div
应该可见。当点击第二个按钮时,相应的div
也应该可见,依此类推。点击按钮应该切换按钮和相应div
的状态。
我不确定是否可以在不使用cookie或本地存储令牌的情况下实现这一点。到目前为止,我创建了以下代码,只允许我在第一次点击时停用div
。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<button class="button" target="1">Button 1</button>
<button class="button" target="2">Button 2</button>
<button class="button" target="3">Button 3</button>
</div>
<div id="mydiv1">
<p>Div 1</p>
</div>
<div id="mydiv2">
<p>Div 2</p>
</div>
<div id="mydiv3">
<p>Div 3</p>
</div>
$(function() {
$('.button').click(function(){
$(this).toggleClass('inactive');
$('#mydiv'+$(this).attr('target')).toggle();
});
});
这段代码通过使用jQuery实现了按钮点击后切换div
的可见性。
英文:
I have several divs which should be shown / hidden when the corresponding button is clicked. In the initial state all buttons and all divs are visible. When the first button is clicked, only the corresponding div should be visible. When the second button is clicked, the corresponding div should be visible as well etc. The click on a button should toggle the state of the button and the corresponding div.
I'm not sure if this can be realized without placing a cookie or local storage token. So far I've created the following code, which only allows me to deactivate the divs with one first click.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(function() {
$('.button').click(function(){
$(this).toggleClass('inactive');
$('#mydiv'+$(this).attr('target')).toggle();
});
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<button class="button" target="1">Button 1</button>
<button class="button" target="2">Button 2</button>
<button class="button" target="3">Button 3</button>
</div>
<div id="mydiv1">
<p>Div 1</p>
</div>
<div id="mydiv2">
<p>Div 2</p>
</div>
<div id="mydiv3">
<p>Div 3</p>
</div>
<!-- end snippet -->
答案1
得分: 3
如果您想要仅切换您刚刚点击的按钮/ div,首先您必须重置所有元素的先前状态为初始状态(删除类inactive
并显示所有div),然后才能切换状态。
$(function() {
$('input[type="checkbox"]').change(function() {
let checked = $('input[type="checkbox"]:checked');
$('.inactive').removeClass('inactive');
if (checked.length == 0) {
$('.subdiv').show();
return;
}
$('input[type="checkbox"]:not(:checked)').closest('label').addClass('inactive');
$('.subdiv').hide();
checked.each(function() {
$('#mydiv' + $(this).val()).toggle();
});
});
});
.inactive {
color: red;
}
label input {
display: none;
}
label {
border: 1px solid gray;
border-radius: 3px;
padding: 2px 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<label><input type="checkbox" value="1"/>Button 1</label>
<label><input type="checkbox" value="2"/>Button 2</label>
<label><input type="checkbox" value="3"/>Button 3</label>
</div>
<div id="mydiv1" class="subdiv">
<p>Div 1</p>
</div>
<div id="mydiv2" class="subdiv">
<p>Div 2</p>
</div>
<div id="mydiv3" class="subdiv">
<p>Div 3</p>
</div>
英文:
If you want to toggle only the button/div that you just clicked, first you have to reset previous state of all elements to initial (remove class inactive
and show all divs) and only than toggle state.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(function() {
$('input[type="checkbox"]').change(function() {
let checked = $('input[type="checkbox"]:checked');
$('.inactive').removeClass('inactive');
if (checked.length == 0) {
$('.subdiv').show();
return;
}
$('input[type="checkbox"]:not(:checked)').closest('label').addClass('inactive');
$('.subdiv').hide();
checked.each(function () {
$('#mydiv' + $(this).val()).toggle();
});
});
});
<!-- language: lang-css -->
.inactive {
color: red;
}
label input {
display: none;
}
label {
border: 1px solid gray;
border-radius: 3px;
padding: 2px 5px;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<label><input type="checkbox" value="1"/>Button 1</label>
<label><input type="checkbox" value="2"/>Button 2</label>
<label><input type="checkbox" value="3"/>Button 3</label>
</div>
<div id="mydiv1" class="subdiv">
<p>Div 1</p>
</div>
<div id="mydiv2" class="subdiv">
<p>Div 2</p>
</div>
<div id="mydiv3" class="subdiv">
<p>Div 3</p>
</div>
<!-- end snippet -->
答案2
得分: 2
你需要一开始隐藏div
,并在点击正确的按钮时显示正确的div
。
$(function() {
$('.button').click(function() {
$(this).toggleClass('active');
$('#mydiv' + $(this).attr('target')).toggle();
});
});
/*
隐藏所有带有以 "mydiv" 开头的 id 的 div
*/
div[id^="mydiv"] {
display: none;
}
.active {
background-color: green;
color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<button class="button" target="1">Button 1</button>
<button class="button" target="2">Button 2</button>
<button class="button" target="3">Button 3</button>
</div>
<div id="mydiv1">
<p>Div 1</p>
</div>
<div id="mydiv2">
<p>Div 2</p>
</div>
<div id="mydiv3">
<p>Div 3</p>
</div>
英文:
You'd have to initially hide the divs and show the correct one when the right button is clicked.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(function() {
$('.button').click(function() {
$(this).toggleClass('active');
$('#mydiv' + $(this).attr('target')).toggle();
});
});
<!-- language: lang-css -->
/*
Hide all divs that have an id starting with "mydiv"
*/
div[id^="mydiv"] {
display: none;
}
.active {
background-color: green;
color: white;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<button class="button" target="1">Button 1</button>
<button class="button" target="2">Button 2</button>
<button class="button" target="3">Button 3</button>
</div>
<div id="mydiv1">
<p>Div 1</p>
</div>
<div id="mydiv2">
<p>Div 2</p>
</div>
<div id="mydiv3">
<p>Div 3</p>
</div>
<!-- end snippet -->
答案3
得分: 0
你是否需要使用一个切换按钮,或者也可以通过其他方式解决它?
英文:
Have you to use a toggle or you can also solve it with a walk around?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(function() {
$('.button').click(function() {
$('.inactive').removeClass('inactive');
$(this).toggleClass('active');
$('.subdiv').show();
$('.mydiv' + $(this).attr('target')).toggle();
});
});
<!-- language: lang-css -->
/*
Hide all divs that have an id starting with "mydiv"
*/
.active {
background-color: green;
color: white;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<button class="button" target="1">Button 1</button>
<button class="button" target="2">Button 2</button>
<button class="button" target="3">Button 3</button>
</div>
<div class="mydiv1" hidden>
<p>Div 1</p>
</div>
<div class="mydiv2" hidden>
<p>Div 2</p>
</div>
<div class="mydiv3" hidden>
<p>Div 3</p>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论