如何更改使用javascript动态显示不同的div

时间:2012-02-16 20:07:06

标签: javascript jquery flot

我是javascripting和jquery的新手。我正在使用flot绘制图表,其中我有2个条形图......

我的第一个条形图

<div id="bar1" style="width:600px;height:300px;"></div>
$(function () {

var css_id = "#bar1";
var data = [
    {label: 'foo',color:'red', data: [[1,300], [2,300], [3,300], [4,300], [5,300]]},
    {label: 'bar',color:'blue', data: [[1,800], [2,600], [3,400], [4,200], [5,0]]},
    {label: 'baz',color:'green', data: [[1,100], [2,200], [3,300], [4,400], [5,500]]},
];
var options = {
    series: {stack: 0,
             lines: {show: false, steps: false },
             bars: {show: true, barWidth: 0.4, align: 'center',},},
    xaxis: {ticks: [[1,'One'], [2,'Two'], [3,'Three'], [4,'Four'], [5,'Five']]},
};

$.plot($(css_id), data, options);

});​

我的第二张图

<div id="bar2" style="width:600px;height:300px;"></div>
var data = [
    {label: 'foo', color:'red', data: [[1,300], [2,300], [3,300], [4,300], [5,300]]},
    {label: 'bar', color:'blue', data: [[1,800], [2,600], [3,400], [4,200], [5,0]]},
    {label: 'baz', color:'yellow', data: [[1,100], [2,200], [3,300], [4,400],   [5,500]]},
    ];

$.plot($("#bar2"), data, {
series: {
    stack: 1,
    bars: {
        show: true,
        barWidth: 0.6,
        fill:1
    }
}
});​

现在我有一个按钮的点击事件

$(document).ready(function() {
......
});

现在我应该怎么写按钮事件的发生,以便我动态地将条形图从<div id="bar1">更改为<div id="bar2">

2 个答案:

答案 0 :(得分:2)

如果我正确理解了这个问题,你想在按下按钮时隐藏/显示每个div ....首先使用<div>隐藏其中一个style="display:none",然后使用以下代码在按钮的click上切换

$(document).ready(function() {
   $('#idofbutton').click(function() {
      $('#bar1,#bar2').toggle();
   });
});

在两者上使用toggle()隐藏当前显示的div并显示当前隐藏的div(希望有意义!)

Docs for toggle()

答案 1 :(得分:0)

#bar2的css设置为display:none

var toggle = true;
$("#button").click(function()
{
    if(toggle)
    {
        $("#bar1").hide();
        $("#bar2").show();
        toggle = false;
    }
    else
    {
        $("#bar1").show();
        $("#bar2").hide();
        toggle = true;
    }

});