2016-03-18 34 views
0

我想用flotr創建一些簡單的可視化效果。然而,當我試圖指定圖表的DIV容器我得到一個錯誤說:Flotr2無法看到jQuery對象

未捕獲的目標容器必須visibleGraph._setEl @ flotr2.min.js:27Graph @ flotr2.min.js:27N .draw @ flotr2.min.js:27(anonymous function)@ lines.html:25i @ jquery.min.js:2j.fireWith @ jquery.min.js:2n.extend.ready @ jquery.min。 JS:2K @ jquery.min.js:2

這項工作但是當我jQuery的$("#chart")選擇更改爲document.getElementById("chart")。基本上,當我使用香草JavaScript的時候可以工作,但是當我切換到jQuery時失敗了。

簡化的娛樂如下。

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <title></title> 
     <script src="JS_libs/flotr2.min.js"></script> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
    </head> 
    <body> 
     <div id='chart' style="width:600px;height:300px;"></div> 
     <!--[if lt IE 9]><script src="js/excanvas.min.js"></script><![endif]--> 

     <script> 
      $(function() { var co2 = [ 
      [ 1959, 315.97 ], 
      [ 1960, 316.91 ], 
      [ 1961, 317.64 ], 
      [ 1962, 318.45 ]]; 
      var temp = [ 
      [ 1959, 0.0776 ], 
      [ 1960, 0.0280 ], 
      [ 1961, 0.1028 ], 
      [ 1962, 0.1289 ]]; 

      Flotr.draw(
      $("#chart"), 
      [{ data: co2, lines: {show:true} }] 
      );}); 
     </script> 

    </body> 
</html> 

如果我改變$("#chart")document.getElementById("chart"),它的工作原理。但我想用jQuery的方式。

回答

0

從jQuery的獲取本地JS對象,你必須使用[0]

Flotr.draw(
    $("#chart")[0], 
    [{ data: co2, lines: {show:true} }] 
);});