如果浏览器不支持HTML 5 Canvas,如何显示图像

时间:2013-12-03 05:53:45

标签: javascript html html5 internet-explorer-8 html5-canvas

我有一些带有Javascript的HTML 5内容,它生成一组带有链接的矩形和文本。 该代码在IE8上不起作用,因为它不支持HTML 5。 如果页面显示在IE8或任何其他不支持HTML5的浏览器中,我希望浏览器显示图像而不是文本。

我尝试了这段代码,但它没有给我预期的输出。

  function draw(){
  canvas = document.getElementById("myCanvas");
  // check if supported
  if(canvas.getContext){

    ctx=canvas.getContext("2d");

    var ctz=canvas.getContext("2d");
    ctz.fillStyle="#FF0000";
    ctz.fillRect(0,0,150,75);

    //..Rest of the code to add html5 content

    }else{
    var img = document.createElement("img");
    img.src = 'http://www.newyorker.com/online/blogs/photobooth/NASAEarth-01.jpg';
    img.width = 276;
    img.height = 110;
    img.alt = '';

    document.body.appendChild(img);

    }
}
draw();

这里的错误是什么?

1 个答案:

答案 0 :(得分:1)

根据canvas element的定义,其内容充当后备内容:实现该元素的浏览器会忽略它,但它会自然地被旧版浏览器处理和显示,而忽略{{1} }和<canvas ...>标签。例如:

</canvas>