且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用相同的script.js文件使用多个ID调用不同的CSS类

更新时间:2023-01-04 17:52:38

如果我正确理解了您的问题,那么您重新绘制硬编码画布元素。参数化抽屉,并使用不同的ID两次调用它。根据此模式更改脚本

If I understood you correctly your problem is that you're drawing in a hardcoded canvas element. Parametrise your drawer and call it two times with different id. Change your script according to this pattern

function drawInCanvas(id){
  var myCanvas = document.getElementById(id);
  myCanvas.width = 50;
  myCanvas.height = 40;

  // and the rest of the code...
}

drawInCanvas("myCanvas1");
drawInCanvas("myCanvas2");