来看看js代码的用法。
我们在使用JavaScript代码有两种写法:一种是在HTML页面中直接嵌入js代码,另一种是通过链接外部的JavaScript文件。
我们先来看第一种用法:在HTML页面中直接嵌入js代码
如果在HTML页面中直接嵌入JavaScript代码,我们需要使用 <script>标签,<script>和 </script>会告诉 JavaScript 在何处开始和结束。
<script>和 </script>之间的代码行包含了 JavaScript:
<script>
alert("My First JavaScript")
</script>
在HTML页面的任何位置(head部分 body部分 body的后面 较好)都可以插入script标签。
你有可能会在 <script>标签中看到使用 type="text/javascript",但是现在已经不必这样做了。JavaScript现在已经是所有现代浏览器以及 HTML5 中的默认脚本语言。
我们来看一个具体的示例
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
document.write("<h1>This is a heading</h1>")
document.write("<p>This is a paragraph</p>")
</script>
</body>
</html>
上述代码就是在在HTML页面中直接嵌入js代码。
接着我们来看看js代码的第二种用法:通过链接外部的JavaScript文件。
有时JavaScript代码过多的话,我们可以将代码单独放在JavaScript文件中然后链接起来。
我们链接外部JavaScript文件可以有以下步骤
1、首先我们建立一个JavaScript文件,扩展名是.js
2、然后将要编写的js代码写到Js文件中,并保存文件。
3、使用script标签将JavaScript文件链接到HTML文件中
<script type="text/javascript" src="JavaScript文件名和路径"></script>
需要注意一点的是,在一个已经引入外部js文件的script标签中,不能在它的开始标签和结束标签中写Js命令了。
我们来看一个具体的示例
我们先建一个js文件sample.js
然后写入js代码
document.write("<p>This is a paragraph</p>")
最后使用script标签将JavaScript文件链接到HTML文件中
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript" src="sample.js"></script>
</body>
</html>
以上就是js代码使用的两种方法
帮你改了一下,能跑起来了。嗯.......你这个有一些语法问题和拼写不仔细的地方,估计太着急,有些粗心了~<!DOCTYPE html>
<html>
<head>
<title>canvas</title>
<style>
*{
padding: 0
margin: 0
}
#container{
height: 80vh
width: 80vw
margin-top: 10vh
margin-left: 10vw
border: 1px solid #aaff66
}
</style>
</head>
<body>
<div id="container">
<canvas id="canvas"></canvas>
</div>
</body>
<script type="text/javascript">
window.onload = function() {
var ctx = document.getElementById('canvas').getContext('2d')
var circle = function(x, y, radius, fillCircle) {
ctx.beginPath()
ctx.arc(x, y, radius, 0, Math.PI * 2, false)
ctx.closePath()
ctx.fillStyle="green"
if (fillCircle) {
ctx.fill()
} else {
ctx.stroke()
}
}
var width=canvas.width = document.getElementById('container').offsetWidth
var height=canvas.height = document.getElementById('container').offsetHeight
var Ball=function(){
this.x=width/2
this.y=height/2
this.xSpeed = 5
this.ySpeed=0
}
Ball.prototype.move=function(){
this.x+=this.xSpeed
this.y+=this.ySpeed
if(this.x<0){
this.x=width
}else if(this.x>width){
this.x=0
}else if(this.y<0){
this.y=height
}else if(this.y>height){
this.y=0
}
}
Ball.prototype.draw=function(){
circle(this.x,this.y,10,true)
}
var ball= new Ball()
setInterval(function(){
ctx.clearRect(0,0,width,height)
ball.draw()
ball.move()
},30)
}
</script>
</html>
尊敬的迅雷用户,您好:建议您先卸载迅雷看看,然后登录迅雷软件中心(http://dl.xunlei.com/)这个页面下载迅雷看看播放器最新版本安装包,重新安装在另外一个磁盘上尝试。(请不要覆盖安装,必须卸载安装,不然可能会覆盖上旧问题)
希望能够帮到您。
感谢您对迅雷的支持!
更多疑问,欢迎您向迅雷网络平台提问。