如规范所述:
The contents of the canvas element, if any, are the element's fallback content.
所以,我认为这样做是合理的:
<head>
<script type="text/javascript">
$(document).ready(function()
{
var canvas = document.querySelector("canvas");
if (canvas != null)
{
var context = canvas.getContext("2d"),
image = canvas.querySelector("img");
// do something special with 'context' and 'image'
}
});
</script>
</head>
<body>
<canvas>
<img class="myImageType" style="border: 1px solid #777;" src="file">
</canvas>
</body>
如您所见,我没有重新创建图像,而是使用了嵌套图像。创建一个新的对我来说没有意义。 但是,样式不会应用于嵌套的图像元素。
最佳答案
找到解决办法:
我需要为 canvas
和嵌套的 img
设置样式,或者向它们添加相同的 class
:
<body>
<canvas class="myImageType">
<img class="myImageType">
</canvas>
</body>
关于css - 如何设置 canvas 元素的后备内容的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8374814/