隐藏<div> onload并在点击事件上显示无法正常工作

时间:2016-10-25 06:33:14

标签: javascript jquery html css echarts

我正在尝试使用display:none和display:block在我的网站上。风格如下:

<style>
  .div_class {
    display: none;
  }
</style>

正文内容为:

<div class="div_class">
   <div id="graph_id"></div>
</div>

脚本是:

    <script>
      // some event occurs and sets the display property of div_class to block as follows
      $(".div_class").css('display','block'); 

      var BarGraph = echarts.init(document.getElementById('graph_id'), theme);
      BarGraph.setOption({  
       // properties that are set for the echart 'graph_id'
      });
    </script>

display: block;使div可见,但设置的echart属性不会呈现。 div显示为空白。可能是什么原因?我无法弄明白。请帮助。谢谢:))

编辑***************************编辑******************* ******************
另外,我认为标题可能会产生误导,因为我可以隐藏div onload并在点击时显示它。但后面的渲染是我遇到某些问题的地方。

4 个答案:

答案 0 :(得分:0)

显示div

后重新呈现BarGraph

答案 1 :(得分:0)

 BarGraph .setOption({

的作用相同
 BarGraph.setOption({

? (摆脱空间)

另外,尝试将所有代码包装在

$(document).ready(function() {
    ***code here***
});

编辑:

https://ecomfe.github.io/echarts-doc/public/tutorial-en.html#Introduction%20of%20ECharts%20features%0D

^从那里获取代码,将jquery添加到head,在div.container中包装图表块,显示:无,创建点击事件

$(document).on('click', function() { $(".container").show(); myChart.setOption(option); });

工作片段:

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ECharts</title>
    <!-- including ECharts file -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="http://echarts.baidu.com/dist/echarts.common.min.js"></script>

</head>
<body>
	<style>
    	.container {
    		display: none;
    	}
    </style>
    <!-- prepare a DOM container with width and height -->
  click anywhere!
    <div class="container">
    	<div id="main" style="width: 600px;height:400px;"></div>
    </div>
    <script type="text/javascript">
        // based on prepared DOM, initialize echarts instance
        var myChart = echarts.init(document.getElementById('main'));

        // specify chart configuration item and data
        var option = {
            title: {
                text: 'ECharts entry example'
            },
            tooltip: {},
            legend: {
                data:['Sales']
            },
            xAxis: {
                data: ["shirt","cardign","chiffon shirt","pants","heels","socks"]
            },
            yAxis: {},
            series: [{
                name: 'Sales',
                type: 'bar',
                data: [5, 20, 36, 10, 10, 20]
            }]
        };

        // use configuration item and data specified to show chart
    	$(document).on('click', function() {
    		$(".container").show();
    		myChart.setOption(option);
    	});
    </script>
</body>
</html>
&#13;
&#13;
&#13;

答案 2 :(得分:-1)

我尝试使用chart.js如果display属性设置为none,则无法绘制图表。事情是visibility:hidden保留空间,而display:none不保留空间

<style>
  .div_class{
      visibility:hidden;
  }
</style>

尝试使用上面的代码替换css。我希望这会对你有所帮助:)。

您可以参考以下帖子获取更多信息:Difference between visibility:hidden and display:none?

答案 3 :(得分:-1)

    <script>
      // some event occurs and sets the display property of div_class to block as follows

      $(".div_class").css('display','block').promise().done(function(){

          //draw graph here

          var BarGraph = echarts.init(document.getElementById('graph_id'), theme);
          BarGraph .setOption({  
           // properties that are set for the echart 'graph_id'
          });

      }); 


    </script>

尝试以上代码。我添加了.promise()它的作用只是在css&#34; display:block;&#34;之后。然后应用图形绘制过程