javascript - Canvas : Getting rid of style attribute

标签 javascript css html canvas

如何去掉canvas自动生成的样式?

Chrome:
<canvas id="g_2" width="604" height="304" style="-webkit-user-select: none; touch-action: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); width: 302px; height: 152px;"></canvas>


Firefox:
<canvas height="157" width="1076" style="-moz-user-select: none; width: 302px; height: 152px;" id="g_2"></canvas>

附言:我没有使用任何 javascript 来添加样式。

为了创建 Canvas ,我使用了 angularJS,它使用数组在 canvas 元素上 ng-repeating,以添加新的 canvas ,索引被附加到该数组。这是代码片段:

<div class="tab-content" data-ng-repeat="tab in someArray">
   <div role="tabpanel" class="tab-pane active">
      <canvas id="someID_[[tab]]"></canvas>
   </div>
</div>

创建canvas元素后,设置宽高如下:

var canvas = document.getElementById(canvasId); 
canvas.width = width; 
canvas.height = height;

最佳答案

当你设置 Canvas 的高度和宽度的同时也设置了它的样式

var canvas = document.getElementById(canvasId); 
canvas.width = width; 
canvas.height = height;
canvas.style = '';

关于javascript - Canvas : Getting rid of style attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35263214/

相关文章:

javascript - 在 Chrome DevTools 如何防止脚本执行

javascript - 使用现有数据格式化新数组 - 问题

css - webpack encore构建报错: Module build failed: Unclosed block

html - 水平导航栏 : How to center text and set colours

javascript - 如何在移动和桌面设备上缩放 Canvas

angularjs - Angular 如何在没有 data-* 前缀的情况下使用自定义属性,而其他人都说你不应该这样做?

javascript - 未捕获的类型错误 : Object [object RegExp] has no method 'match'

javascript - NgZone 在 Angular 4 中没有按预期工作

css - RSS 不在 Chrome 中显示

javascript - 获取 PHP URL 的 JS 值