代码仅适用于jsfiddle

时间:2015-04-09 08:56:57

标签: javascript jquery html css

我遇到一些代码问题。 它只在jsfiddle运行,我不知道它为什么不在codepen或文件中工作。

为什么此代码仅适用于jsfiddle? 我在codepen和文件中尝试过,但它没有显示任何内容 http://jsfiddle.net/auagufL8/

<script src="http://code.highcharts.com/maps/highmaps.js"></script>
<script src="http://code.highcharts.com/maps/modules/exporting.js"></script>
<script src="http://code.highcharts.com/mapdata/countries/nl/nl-all.js">   </script>

<div id="container"></div>


$(function () {

// Prepare demo data
var data = [
    {
        "hc-key": "nl-fr",
        "value": 0
    },
    {
        "hc-key": "nl-gr",
        "value": 1
    },
    {
        "hc-key": "nl-fl",
        "value": 2
    },
    {
        "hc-key": "nl-ze",
        "value": 3
    },
    {
        "hc-key": "nl-nh",
        "value": 4
    },
    {
        "hc-key": "nl-zh",
        "value": 5
    },
    {
        "hc-key": "nl-dr",
        "value": 6
    },
    {
        "hc-key": "nl-ge",
        "value": 7
    },
    {
        "hc-key": "nl-li",
        "value": 8
    },
    {
        "hc-key": "nl-ov",
        "value": 9
    },
    {
        "hc-key": "nl-nb",
        "value": 10
    },
    {
        "hc-key": "nl-ut",
        "value": 11
    }
];

// Initiate the chart
$('#container').highcharts('Map', {

    title : {
        text : 'Highmaps basic demo'
    },

    subtitle : {
        text : 'Source map: <a href="http://code.highcharts.com/mapdata/countries/nl/nl-all.js">The Netherlands</a>'
    },

    mapNavigation: {
        enabled: true,
        buttonOptions: {
            verticalAlign: 'bottom'
        }
    },

    colorAxis: {
        min: 0
    },

    series : [{
        data : data,
        mapData: Highcharts.maps['countries/nl/nl-all'],
        joinBy: 'hc-key',
        name: 'Random data',
        states: {
            hover: {
                color: '#BADA55'
            }
        },
        dataLabels: {
            enabled: true,
            format: '{point.name}'
        }
    }]
});
 });


#container {
height: 500px; 
min-width: 310px; 
max-width: 800px; 
margin: 0 auto; 
}
.loading {
margin-top: 10em;
text-align: center;
color: gray;
}

4 个答案:

答案 0 :(得分:0)

要运行代码是一个浏览器,您需要将其放在<script> //Your code goes here </script>标记

答案 1 :(得分:0)

这 - &gt;

#container {
    height: 500px; 
    min-width: 310px; 
    max-width: 800px; 
    margin: 0 auto; 
    }
    .loading {
    margin-top: 10em;
    text-align: center;
    color: gray;
    }

是CSS,它必须位于<style>标记内。

这 - &gt;

$(function () {

// Prepare demo data
var data = [
    {
        "hc-key": "nl-fr",
        "value": 0
    },
    {
        "hc-key": "nl-gr",
        "value": 1
    },
    {
        "hc-key": "nl-fl",
        "value": 2
    },
    {
        "hc-key": "nl-ze",
        "value": 3
    },
    {
        "hc-key": "nl-nh",
        "value": 4
    },
    {
        "hc-key": "nl-zh",
        "value": 5
    },
    {
        "hc-key": "nl-dr",
        "value": 6
    },
    {
        "hc-key": "nl-ge",
        "value": 7
    },
    {
        "hc-key": "nl-li",
        "value": 8
    },
    {
        "hc-key": "nl-ov",
        "value": 9
    },
    {
        "hc-key": "nl-nb",
        "value": 10
    },
    {
        "hc-key": "nl-ut",
        "value": 11
    }
];

// Initiate the chart
$('#container').highcharts('Map', {

    title : {
        text : 'Highmaps basic demo'
    },

    subtitle : {
        text : 'Source map: <a href="http://code.highcharts.com/mapdata/countries/nl/nl-all.js">The Netherlands</a>'
    },

    mapNavigation: {
        enabled: true,
        buttonOptions: {
            verticalAlign: 'bottom'
        }
    },

    colorAxis: {
        min: 0
    },

    series : [{
        data : data,
        mapData: Highcharts.maps['countries/nl/nl-all'],
        joinBy: 'hc-key',
        name: 'Random data',
        states: {
            hover: {
                color: '#BADA55'
            }
        },
        dataLabels: {
            enabled: true,
            format: '{point.name}'
        }
    }]
});
 });

是Javascript,它必须位于<script>标记内。

答案 2 :(得分:0)

<强>喔!

这段代码无处不在。
要使它在Codepen中运行,您需要通过JS面板中的小cog 添加所有JS资源,并检查是否已选中Latest version of jQuery。 HTML面板中会忽略<script>个标记。

&#13;
&#13;
$(function () {

    // Prepare demo data
    var data = [
        {"hc-key": "nl-fr","value": 0},
        {"hc-key": "nl-gr","value": 1},
        {"hc-key": "nl-fl","value": 2},
        {"hc-key": "nl-ze","value": 3},
        {"hc-key": "nl-nh","value": 4},
        {"hc-key": "nl-zh","value": 5},
        {"hc-key": "nl-dr","value": 6},
        {"hc-key": "nl-ge","value": 7},
        {"hc-key": "nl-li","value": 8},
        {"hc-key": "nl-ov","value": 9},
        {"hc-key": "nl-nb","value": 10},
        {"hc-key": "nl-ut","value": 11}
    ];

    // Initiate the chart
    $('#container').highcharts('Map', {
        title : {
            text : 'Highmaps basic demo'
        },
        subtitle : {
            text : 'Source map: <a href="http://code.highcharts.com/mapdata/countries/nl/nl-all.js">The Netherlands</a>'
        },
        mapNavigation: {
            enabled: true,
            buttonOptions: {
                verticalAlign: 'bottom'
            }
        },
        colorAxis: {
            min: 0
        },
        series : [{
            data : data,
            mapData: Highcharts.maps['countries/nl/nl-all'],
            joinBy: 'hc-key',
            name: 'Random data',
            states: {
                hover: {
                    color: '#BADA55'
                }
            },
            dataLabels: {
                enabled: true,
                format: '{point.name}'
            }
        }]
    });
});
&#13;
#container {
    height: 500px; 
    min-width: 310px; 
    max-width: 800px; 
    margin: 0 auto; 
}
.loading {
    margin-top: 10em;
    text-align: center;
    color: gray;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/maps/highmaps.js"></script>
<script src="http://code.highcharts.com/maps/modules/exporting.js"></script>
<script src="http://code.highcharts.com/mapdata/countries/nl/nl-all.js"></script>

<div id="container"></div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

将所有与CSS相关的文件放在单独的.css文件中,或将其放在HTML文件中的标记下。还要将所有脚本放在.js文件中,或将其嵌入HTML文件中的标记下。

如需更多参考,您可以访问this旧帖子。