[英]Disable button 1 Section when click on button 2
Have multiple buttons and sections associated with those buttons in the Elementor page builder.在 Elementor 页面构建器中有多个按钮和与这些按钮关联的部分。
What I am trying to achieve is when page loads, we can only see buttons and section will toggle when someone clicks on the button.我想要实现的是当页面加载时,我们只能看到按钮,当有人点击按钮时,部分会切换。
I have managed to achieve that, but when clicking on second button, button 1 section should hide and button 2 section should display.我已经设法实现了这一点,但是当单击第二个按钮时,按钮 1 部分应该隐藏,而按钮 2 部分应该显示。
Here is the w3 schools link for my project这是 我的项目的 w3 学校链接
jQuery(document).ready(function( $ ){ var hbtn = $(".coursesBtn"); var hcon = $(".coursesSection"); hcon.hide(); hbtn.click(function(e) { var index = hbtn.index(this) $(hcon).eq(index).slideToggle("slow"); e.preventDefault(); }); }); jQuery(document).ready(function( $ ){ var hbtn = $(".videosBtn"); var hcon = $(".videosSection"); hcon.hide(); hbtn.click(function(e) { var index = hbtn.index(this) $(hcon).eq(index).slideToggle("slow"); e.preventDefault(); }); });
You can use onclick
for buttons for example例如,您可以使用
onclick
作为按钮
<button class="btn1" onclick="f1()">BUTTON1</button>
and add the function to toggle in f1()
并添加 function 以切换
f1()
Here is a quick example, the idea is to hide the other sections first, and show the section that you want to be displayed;这是一个简单的例子,想法是先隐藏其他部分,然后显示您想要显示的部分;
$(document).ready(function($) { $('.btn1').click(function() { $('.section').hide(); $('.section1').show(); }) $('.btn2').click(function() { $('.section').hide(); $('.section2').show(); }) $('.btn3').click(function() { $('.section').hide(); $('.section3').show(); }) })
.section { width: 100%; padding: 50px 0; text-align: center; background-color: lightblue; margin-top: 20px; }.section2 { background-color: lightgreen; }.section3 { background-color: orange; }
<button class="btn1">BUTTON1</button> <button class="btn2">BUTTON2</button> <button class="btn3">BUTTON3</button> <div class="section section1"> This is Section1. </div> <div class="section section2"> This is Section2. </div> <div class="section section3"> This is Section3. </div> <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
Add a new class for each button ( .btn
) and section ( .section
) and use these as in the code below.为每个按钮 (
.btn
) 和部分 ( .section
) 添加一个新的 class 并在下面的代码中使用它们。 You can have as many buttons and sections as you want and the code below will work without any modification:您可以拥有任意数量的按钮和部分,下面的代码无需任何修改即可工作:
var hbtn = $(".btn");
var hcon = $(".section");
hcon.hide();
hbtn.click(function(e) {
var index = hbtn.index(this);
hcon.hide() //OR hcon.filter(':visible').slideToggle("slow")
.eq(index).slideToggle("slow");
});
DEMO演示
$(document).ready(function( $ ){ var hbtn = $(".btn"); var hcon = $(".section"); hcon.hide(); hbtn.click(function(e) { var index = hbtn.index(this); hcon.hide() //OR hcon.filter(':visible').slideToggle("slow").eq(index).slideToggle("slow"); }); });
.section1 { width: 100%; padding: 50px 0; text-align: center; background-color: lightblue; margin-top: 20px; }.section2 { width: 100%; padding: 50px 0; text-align: center; background-color: lightblue; margin-top: 20px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button class="btn1 btn">BUTTON1</button> <button class="btn2 btn">BUTTON2</button> <div class="section1 section"> This is Section1. </div> <div class="section2 section"> This is Section2. </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.