Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

javascript - 如何将JavaScript文件链接到ID(How to link javascript file to an id)

I have a javascript code called static.js, this works great on its own when it is sourced in the html file as such:(我有一个名为static.js的javascript代码,当它来自html文件时,它就可以很好地工作:)

<script src="./static.js"></script>

The code executes and you can see it on the screen, however I would like add effects on to this output.(代码将执行,您可以在屏幕上看到它,但是我想在此输出上添加效果。)

How can I associate an entire javascript file to a <canvas> or an id.(如何将整个JavaScript文件关联到<canvas>或ID。) So that I put it between <div> and layer effects on top of it.(因此,我将其放在<div>和它上面的图层效果之间。)   ask by Joe translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Looking at this article , It seems that you would create the tag in your index.html file, and the you would be able to control it in the JavaScript file like so, assuming that the id of the canvas element is "myCanvas":(看一下本文 ,似乎您可以在index.html文件中创建标记,并且您可以像这样在JavaScript文件中对其进行控制,假设canvas元素的ID为“ myCanvas”:)

var canvasVariable = document.getElementById("myCanvas");

And then you would be able to add effects to that canvas by using methods like:(然后,您可以使用以下方法向该画布添加效果:)

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.moveTo(0,0);
ctx.lineTo(200,100);
ctx.stroke();

This example is from the aforementioned article and creates a diagonal line across the canvas.(该示例来自上述文章,并在画布上创建了一条对角线。)

Also, for future reference, mention that you are using jquery.(另外,为了将来参考,请提及您正在使用jquery。)

Thankfully I recognized the error and imported it.(值得庆幸的是,我认识到错误并将其导入。)

I hope this was able to help!(我希望这能够有所帮助!)

phylo(phylo)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...