[事件处理] 点击同一按钮实现div的隐藏与显示切换

点击同一按钮实现div的隐藏与现实切换:
在很多应用中,都有这样的功能,点击同一个按钮可以实现div的隐藏或者现实,当然操作的并非必须是按钮或者div,不过原理是一样的,下面就通过代码实例介绍一下如何实现此功能。
代码如下:
[HTML]  纯文本查看  复制代码 运行代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!DOCTYPE html>
< html >
< head >
< meta charset = " utf-8" >
< meta name = "author" content = "http://www.softwhy.com/" />
< title >蚂蚁部落</ title >
< style type = "text/css" >
#thediv{
   width:250px;
   height:50px;
   background-color:red;
}
</ style >
< script type = "text/javascript" src = "mytest/jQuery/jquery-1.8.3.js" ></ script >
< script type = "text/javascript" >
$(document).ready(function(){
   $("#bt").toggle(function(){
     $("#thediv").hide()
     $("#bt").val("点击显示");
   },function(){
     $("#thediv").show()
     $("#bt").val("点击隐藏");
   })  
});
</ script >
</ head >
< body >
< div id = "thediv" ></ div >
< input type = "button" id = "bt" value = "点击隐藏" />
</ body >
</ html >

以上代码可以实现我们想要的效果,这里主要利用了toggle()函数。
特别说明: toggle()函数已经在jQuery1.9中被删除。
相关阅读:
1.toggle()函数可以参阅 jQuery的toggle()方法 一章节。
2.hide()函数可以参阅 jQuery的hide()方法 一章节。
3.val()函数可以参阅 jQuery的val()方法 一章节。
4.show()函数可以参阅 jQuery的show()方法 一章节。 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现在echarts中用el-radio-button按钮切换不同的数据,可以先在vue中定义一个变量来存储当前选中的数据源,例如: ``` <template> <div> <el-radio-group v-model="selectedSource"> <el-radio-button :label="source1">数据源1</el-radio-button> <el-radio-button :label="source2">数据源2</el-radio-button> <el-radio-button :label="source3">数据源3</el-radio-button> </el-radio-group> <div id="chart"></div> </div> </template> <script> export default { data() { return { selectedSource: 'source1', // 默认选中数据源1 source1: [], // 数据源1 source2: [], // 数据源2 source3: [], // 数据源3 } }, methods: { fetchData() { // 使用fetch获取数据,并将返回的数据赋值给对应的数据源 fetch('/api/data1').then(response => response.json()).then(data => this.source1 = data) fetch('/api/data2').then(response => response.json()).then(data => this.source2 = data) fetch('/api/data3').then(response => response.json()).then(data => this.source3 = data) }, renderChart() { // 根据当前选中的数据源渲染图表 const data = this[this.selectedSource] // 使用echarts绘制图表 const chart = echarts.init(document.getElementById('chart')) chart.setOption({ // 配置图表选项 series: [ { type: 'bar', data, }, ], }) }, }, mounted() { this.fetchData() // 获取数据 this.renderChart() // 渲染图表 }, watch: { selectedSource() { this.renderChart() // 监听选中的数据源变化,重新渲染图表 }, }, } </script> ``` 在上面的代码中,我们定义了一个selectedSource变量来存储当前选中的数据源,然后在模板中使用el-radio-button按钮来选择不同的数据源。在mounted钩子函数中,我们调用fetchData方法来获取数据,在renderChart方法中根据当前选中的数据源渲染图表。同时,我们使用watch监听selectedSource变量的变化,一旦变化就重新渲染图表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值