gpt4 book ai didi

javascript - 在html中调用js方法

转载 作者:行者123 更新时间:2023-11-30 15:37:23 24 4
gpt4 key购买 nike

我对 js 中的类以及如何在 html 文件中调用它有疑问。

class SetProp(){
constructor(height,width){
this.height = height;
this.width = width;
}
function getHeight(){
return this.height;
}
function getWidth(){
return this.width;
}
}
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
<script src="TryClass.js" type="text/javascript">
prop = new SetProp(1000,1000);
</script>
</head>
<body>
<h1>Hello</h1>
<canvas id="canvas" width="prop.getWidth() " height="prop.getHeight();" style="border:1px solid #000000"></canvas>
</body>
</html>

我想用这个方法制作 1000*1000 的 Canvas 。并且需要练习使用类。

最佳答案

html 属性 widthheight 不能分配给函数,你需要做的是编写一个函数,在按钮点击时或之后返回宽度和高度DOM 加载使用选择器使用 Javascript 修改元素的属性,如下所示:

document.getElementById('canvas').style.width =prop.getWidth() ;
document.getElementById('canvas').style.height =prop.getHeight() ;

关于javascript - 在html中调用js方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41319336/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com