问题
I was wondering if it was possible to render the same chart to multiple divs with the same ID. I am just using this as a template to show how this stuff functions, ultimately, i'll tie into this and dynamically create the divs on the fly and can have unique IDs for each of them, but rather than just copy/paste the chart code 4 times i'd like the same chart to render in each of the divs. I've tried using the .each() function but it still only renders in the first div.
Here is a fiddle rather than copy/paste all of that code and a short snippet of the each function; http://jsfiddle.net/Chmts/58/
$(function () {
$(document).ready(function(){
$('#CampaignPercent').each(function(){
var chart = new Highcharts.Chart({
chart: {
renderTo: this,
type: 'bar',
width: 200,
margin: [0,0,0,0]
},
回答1:
IDs should be always unique, $('#CampaignPercent')
will only return first element with id CampaignPercent
. you should rather use common classname(say CampaignPercent
) and then use class selector .
to target all the elements:
$('.CampaignPercent').each(function(){
var chart = new Highcharts.Chart({
chart: {
renderTo: this,
type: 'bar',
width: 200,
margin: [0,0,0,0]
},
Working Demo
来源:https://stackoverflow.com/questions/29921507/rendering-highcharts-chart-in-multiple-divs-with-same-id