1.代码片段
<style>
.box{
width: 400px;
height: 400px;
margin: 40px auto;
}
</style>
<div class="box"></div>
<script type="text/javascript">
var myChart = echarts.init(document.getElementById("box"));
</script>
2.在引入echarts报错:Uncaught TypeError: Cannot read property 'getAttribute' of null。HTML中的body还没有执行,会出现获取不到div的属性。
有两种解决办法:
1.把div的样式改为内联样式,如:<div id="box" style="width: 400px;height: 400px;margin: 40px auto;"></div>
2.改变获取div的方法,如:
var myChart=echarts.init(document.querySelector('.box'));