javascript - 单击按钮 2 时禁用按钮 1 部分
问题描述
在 Elementor 页面构建器中有多个按钮和与这些按钮关联的部分。
我想要实现的是当页面加载时,我们只能看到按钮,当有人点击按钮时,部分会切换。
我已经设法实现了这一点,但是当单击第二个按钮时,按钮 1 部分应该隐藏,而按钮 2 部分应该显示。
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();
});
});
解决方案
这是一个简单的例子,想法是先隐藏其他部分,然后显示您想要显示的部分;
$(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>
推荐阅读
- jquery - 使用 webpack 时 jQuery 插件不起作用。什么是正确的使用方法?
- cassandra - 替换 logback 中的多个值
- c# - 如何以 sudo 运行应用程序
- java - Spring:在 Dispatcher Servlet 中找不到带有 URI [uri] 的 HTTP 请求的映射
- hyperledger-fabric - 如何从 NodeSDK 中的查询请求中从 shim.Error("Error Message") 获取“错误消息”
- javascript - 使用php在数据输入期间防止重复输入
- search - 带有每个选项结果计数的搜索菜单
- php - 使用 codeigniter 在同一视图页面中编辑和添加
- android - Android水平轮播RecyclerView消失
- python - Python图形绘图问题