1、错误描述:
echarts.js:1136 Uncaught Error: Initialize failed: invalid dom.
2、错误原因
<script>
var data=getdata(); $.get('js/shandong.json', function (shandongJson) { var chart = echarts.init(document.getElementById('main1')); chart.setOption({ title: { text: '山东省化工园区分布图', left: 'center' }, tooltip : { padding: 10, backgroundColor: '#222', borderColor: '#777', borderWidth: 1, formatter: function (obj) { var value = obj.value; return '<div style="border-bottom: 1px solid rgba(255,255,255,.3); font-size: 18px;padding-bottom: 7px;margin-bottom: 7px">'+ obj.name +'</div>' + '规划面积:' + value[3] + '平方公里<br>' + '主导产业:' + value[4] + '<br>'; } }, bmap: { center: [118.804129, 36.400339], zoom: 7, roam: true, mapStyle: { styleJson: [{ 'featureType': 'water', 'elementType': 'all', 'stylers': { 'color': '#E2F2FC' } }, { 'featureType': 'land', 'elementType': 'all', 'stylers': { 'color': '#f3f3f3' } }, { 'featureType': 'railway', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'highway', 'elementType': 'all', 'stylers': { 'color': '#fdfdfd' } }, { 'featureType': 'highway', 'elementType': 'labels', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'arterial', 'elementType': 'geometry', 'stylers': { 'color': '#fefefe' } }, { 'featureType': 'arterial', 'elementType': 'geometry.fill', 'stylers': { 'color': '#fefefe' } }, { 'featureType': 'poi', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'green', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'subway', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'manmade', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'local', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'arterial', 'elementType': 'labels', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'boundary', 'elementType': 'all', 'stylers': { 'color': '#fefefe' } }, { 'featureType': 'building', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'label', 'elementType': 'labels.text.fill', 'stylers': { 'color': '#999999' } }, { "featureType": "boundary", "elementType": "geometry", "stylers": { "color": "#ff0000", "weight": "4", "visibility": "on" } }] } }, series : [ { name: 'Top 5', type: 'effectScatter', coordinateSystem: 'bmap', data: data, symbolSize: function (val) { return val[3] / 2; }, showEffectOn: 'render', rippleEffect: { brushType: 'stroke' }, hoverAnimation: true, label: { normal: { formatter: '{b}', position: 'right', show: true } }, itemStyle: { normal: { color: '#1ABC9C', shadowBlur: 10, shadowColor: '#333' } }, zlevel: 1 } ] }); });
</script>
获取不到页面的id为main1的元素;
3、解决办法:将echart加载代码放在$(document).ready(function(){……})中即可正常;