【问题标题】:canvas.getContext("2d") is undefinedcanvas.getContext("2d") 未定义
【发布时间】:2013-06-23 16:44:33
【问题描述】:

以下代码在所有浏览器中都可以正常打开,但是当我尝试在 WPF Web 浏览器控件中运行代码时,它会给出 Javascript 错误“canvas.getContext("2d") is undefined”。

<html>
   <head>
      <title>Bar Chart</title>
      <script src="Chart.js" type="text/javascript"></script>  
      <script type="text/javascript">
       var canvas = null;
       var context = null;

       window.onload = function () {
        invokeService();
        canvas = document.getElementById("canvas");
        var context = canvas.getContext("2d");
        alert(context);
        var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Bar(barChartData);
    };

    var barChartData;
    function invokeService() {
        alert("q");
        barChartData = {
            labels: ["January", "February", "March", "April", "May", "June", "July"],
            datasets: [
                       {
                           fillColor: "rgba(220,220,220,0.5)",
                           strokeColor: "rgba(220,220,220,1)",
                           data: [65, 59, 90, 81, 56, 55, 40]
                       },
                       {
                           fillColor: "rgba(151,187,205,0.5)",
                           strokeColor: "rgba(151,187,205,1)",
                           data: [28, 48, 40, 19, 96, 27, 100]
                       }
                 ]
        }
    }
    </script>  
 </head>
 <body>
     <canvas id="canvas" height="450" width="600"></canvas>
 </body>
</html>

编辑1:chart.js可以在这里下载https://github.com/nnnick/Chart.js

【问题讨论】:

    标签: html wpf canvas


    【解决方案1】:

    喝完一杯咖啡后,我通过添加以下行解决了这个问题

    <meta http-equiv="X-UA-Compatible" content="IE=9">
    

    【讨论】:

    • 这很好。我知道当 IE 作为WebBrowser 控件托管时,it defaults to IE7 mode(假设它是一个旧的关键业务线应用程序;不能冒险进入 IE9 模式,也不能轻易更改为强制 IE=7)。 “WPF Web 浏览器” 真的只是托管在 WPF 应用程序中的 Internet Explorer 吗?
    • 来自user3779105的问题:你到底是在哪里添加标签的?
    • @ArtjomB。在&lt;head&gt;...&lt;/head&gt;
    猜你喜欢
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    相关资源
    最近更新 更多