代码只能在jsfiddle中工作

Code only works in jsfiddle

本文关键字:工作 jsfiddle 代码      更新时间:2023-09-26

我的一些代码有问题。它只在jsfiddle中运行,我不知道为什么它不能在代码笔或文件中运行。

为什么这个代码只在jsfiddle中工作?我在代码笔和文件中都试过了,但什么都没显示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;
}

这段代码在任何地方都能完美工作
为了让它在Codepen中工作,您需要通过JS面板内的小齿轮添加所有JS资源,并检查是否选择了Latest version of jQuery<script>标记在HTML面板中被忽略。

$(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;
}
<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>

要运行浏览器代码,需要将其放入<script> //Your code goes here </script>标记

this->

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

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

这个->

$(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>标记内。

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

要获得更多参考,你可以访问这篇旧帖子。