<html>
<head>
<title>JavaScript练习</title>
</head>
<body>
<h2>JavaScript是什么?</h2>
<p>JavaScript是基于对象和事件驱动并具有安全性能的脚本语言。</p>
<p>与HTML超文本标记语言、CSS样式一起实现在一个web页面中与客户进行交互,
它是通过嵌入代码和调用外部文件是实现的。
</p>
<hr color="red">
<h2>网页中调用JavaScript的方式</h2>
<p>方式一:使用Script标记</p>
<Script>
alert("你好,很高兴遇见你!")
</Script>
<p>方式二:使用外部文件</p>
<Script src="test.js"></Script>
<p>方式三:在事件处理中使用JavaScript</p>
<input type="button" value="yes" οnclick="alert('are you sure!');" style="width=60;height=30;background-color='blue';border-color='red'"/>
<hr color="red">
<h2>JavaScript的基本语法</h2>
<ol type="I">
<li><h3>变量</h3>
<p>变量的声明和赋值</p>
<pre>JS的基本数据类型包括:1、数值型(整数、小数) 2、字符串型(用双引号)
3、字符型(用单引号) 4、布尔型(true和false) 5、对象型 6空值null
</pre>
<Script>
var a=20;//全局变量:定义于函数体之外,作用于整个网页
function fun () {
var a=30;//局部变量;定义于函数体之内,作用于函数体中
alert("局部变量a="+a);
}
fun();
alert("全局变量a="+a);
</Script>
<hr color="red">
<li><h3>运算符号</h3>
<p>算术运算符</p>
<p>比较运算符</p>
<p>逻辑运算符</p>
<p>typeof运算符</p>
<hr color="red">
<li><h3>控制语句</h3>
<p>条件语句:if,switch</p>
<p>循环语句:for,while</p>
年龄
<select name="age">
<script>
for (i = 18; i < 100; i++) {//i是全局变量
// code to be executed
document.write("<option value="+i+">"+i+ "</option>");
}
</script>
</select>
<br><br>
<p>九九乘法表</p>
<table border=1px >
<script>
for(i=1;i<10;i++){
document.write("<tr>");
for(j=1;j<=i;j++){
document.write("<td>");
document.write(i+"*"+j+"="+(i*j)+"\n" );
document.write("</td>");
}
document.write("<br>");
document.write("</tr>");
}
</script>
</table>
<br><br>
<p>创建5行10列表格</p>
<table width=400px border=1px >
<script>
for(i=0;i<5;i++){
document.write("<tr>");
for(j=0;j<10;j++){
document.write("<td>"+(i*j)+"</td>");
}
document.write("</tr>");
}
</script>
</table>
<hr color="red">
<li><h3>内置函数</h3></h3>
<script>
alert("string");//弹出对话框,显示写入的内容
parseInt("String");//将字符串转为整数
parseFloat("String");//将字符串转为浮点数
isNaN("String");//判断是否为数字,是数字为false
document.write("数学函数"+"<br>");
document.write("Math.PI="+Math.PI+"<br>");
document.write("Math.random()="+Math.random()+"<br>");//随机数,0-1
document.write("Math.sin(Math.PI/6)="+Math.sin(Math.PI/6)+"<br>");//正弦函数
document.write("Math.pow(2,10)="+Math.pow(2,10)+"<br>");//2的10次方
document.write("Math.sqrt(16)="+Math.sqrt(16)+"<br>");//16的开方
document.write("字符串处理函数"+"<br>");
var s="Hello,World!";
document.write("s.length="+s.length+"<br>");//字符串长度
document.write("s.charAt(3)="+s.charAt(3)+"<br>");//(0开始)排序第三的字符
document.write("s.toLowerCase()="+s.toLowerCase()+"<br>");//小写
document.write("s.toUpperCase()="+s.toUpperCase()+"<br>");//大写
var k=s.split(",");//用逗号切割
document.write("k[0]="+k[0]+"<br>");//数组k的第一个元素
document.write("k[1]="+k[1]+"<br>");//数组k的第二个元素
</script>
</ol>
</body>
</html>
JavaScript基础
最新推荐文章于 2022-11-02 12:49:00 发布