2015-12-01 172 views
1

我正在嘗試將div標籤中的內容轉換爲Javascript代碼。其原因是採取div信息並將其轉換爲Highchart數據系列。將HTML內容轉換爲Javascript代碼

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 /片斷內容

$(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()] 
 
     }] 
 

 
    }); 
 
});
<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>

它拉內容爲文本,並且不產生它作爲代碼。這可以做到嗎?

+0

是否可以爲你生成一個腳本標籤,而不是爲數據,在其中可以你可以創建一個JS變量爲您的數據一個div – gunnx

+0

你可以調用JSON.parse創建對象,我可能會建議使用例如這個,'

0

你可以嘗試eval函數

的eval()函數的計算結果表示爲一個字符串的JavaScript代碼。

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

這真是**危險**,並且在這種情況下'JSON.parse'完美無缺。 – LGSon

+0

同意但是有些瀏覽器可能不支持JSON方法而且JSON解析也使用eval來執行代碼,但它在執行前驗證代碼更安全,無論如何感謝 – thepiyush13

+1

[JSON.parse瀏覽器支持幾乎100%](http:///caniuse.com/#feat=json),那麼它是100%,因爲可以使用[this library](http://www.json.org/js.html) – LGSon