Rendering Highcharts Chart in Multiple divs with same ID

瘦欲@ 提交于 2019-12-13 07:57:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!