HTML内容到Javascript代码

时间:2015-12-01 22:08:30

标签: javascript html highcharts

我正在尝试将内容放在div标记中并将其转换为Javascript代码。原因是获取div信息并将其转换为Highchart数据系列。

HTML内容

<div id="data">{name: 'Point 1',x: Date.UTC(2014, 11, 1),x2: Date.UTC(2014, 11, 8),y: 0},{name: 'Point 2',x: Date.UTC(2014, 12, 1),x2: Date.UTC(2014, 12, 8),y: 0},</div>

Javascript /代码段内容

&#13;
&#13;
$(function () {
var newdata = $("#data");

     (function (H) {
        var defaultPlotOptions = H.getOptions().plotOptions,
            columnType = H.seriesTypes.column,
            each = H.each;

        defaultPlotOptions.xrange = H.merge(defaultPlotOptions.column, {});
        H.seriesTypes.xrange = H.extendClass(columnType, {
            type: 'xrange',
            parallelArrays: ['x', 'x2', 'y'],
            requireSorting: false,
            animate: H.seriesTypes.line.prototype.animate,

            /**
             * Borrow the column series metrics, but with swapped axes. This gives free access
             * to features like groupPadding, grouping, pointWidth etc.
             */
            getColumnMetrics: function () {
                var metrics,
                    chart = this.chart;

                function swapAxes() {
                    each(chart.series, function (s) {
                        var xAxis = s.xAxis;
                        s.xAxis = s.yAxis;
                        s.yAxis = xAxis;
                    });
                }

                swapAxes();

                this.yAxis.closestPointRange = 1;
                metrics = columnType.prototype.getColumnMetrics.call(this);

                swapAxes();

                return metrics;
            },
            translate: function () {
                columnType.prototype.translate.apply(this, arguments);
                var series = this,
                    xAxis = series.xAxis,
                    metrics = series.columnMetrics;

                H.each(series.points, function (point) {
                    var barWidth = xAxis.translate(H.pick(point.x2, point.x + (point.len || 0))) - point.plotX;
                    point.shapeArgs = {
                        x: point.plotX,
                        y: point.plotY + metrics.offset,
                        width: barWidth,
                        height: metrics.width
                    };
                    point.tooltipPos[0] += barWidth / 2;
                    point.tooltipPos[1] -= metrics.width / 2;
                });
            }
        });

        /**
         * Max x2 should be considered in xAxis extremes
         */
        H.wrap(H.Axis.prototype, 'getSeriesExtremes', function (proceed) {
            var axis = this,
                dataMax = Number.MIN_VALUE;

            proceed.call(this);
            if (this.isXAxis) {
                each(this.series, function (series) {
                    each(series.x2Data || [], function (val) {
                        if (val > dataMax) {
                            dataMax = val;
                        }
                    });
                });
                if (dataMax > Number.MIN_VALUE) {
                    axis.dataMax = dataMax;
                }
            }
        });
    }(Highcharts));


    // THE CHART
    $('#container').highcharts({
        chart: {
            type: 'xrange'
        },
        title: {
            text: 'Highcharts X-range study'
        },
          plotOptions: {
        series: {
            events: {
                mouseOver: function () {
                    var cur = this;
                    Highcharts.each(this.chart.series, function (series) {
                        if (series !== cur) {
                            series.group.animate({
                                opacity: 0.2
                            }, {
                                duration: 150
                            });
                        } else {
                            series.group.animate({
                                opacity: 1
                            }, {
                                duration: 150
                            });
                        }   
                    });
                },
                mouseOut: function () {
                    this.group.animate({
                        opacity: 1
                    }, {
                        duration: 150
                    });
                }   
            }
        }
    },
        xAxis: {
            type: 'datetime'
        },
        yAxis: {
            title: '',
            categories: [],
        },
        series: [$(newdata).text()]
        }]

    });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div id="data">{name: 'Point 1',x: Date.UTC(2014, 11, 1),x2: Date.UTC(2014, 11, 8),y: 0},{name: 'Point 2',x: Date.UTC(2014, 12, 1),x2: Date.UTC(2014, 12, 8),y: 0},</div>
&#13;
&#13;
&#13;

它将内容作为文本提取,不会将其创建为代码。可以这样做吗?

2 个答案:

答案 0 :(得分:1)

如果#data元素的所有内容都已正确格式化,那么您实际需要做的就是获取该元素的内部html并将其解析为JSON。

// This is the contents of #data as a String
var data_as_a_string = document.getElementById("data").innerHTML;

// And here it is "in javascript" as an Object
var data_as_an_object = JSON.parse( data_as_a_string );

答案 1 :(得分:0)

你可以试试eval功能

  

eval()函数评估表示为字符串的JavaScript代码。

eval(string)
eval(" var s='hello'; alert(s);");