JavaScript基础-2

    javascript的基本语法,javascript具有所有语言都具有的所有程序结构:顺序/分支/循环这三种结构。

if语句的例子:

   1: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
   2: <html>
   3:     <head>
   4:         <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   5:         <title>Untitled Document</title>
   6:         <script type="text/javascript">
   1:  
   2:             function testIF(){
   3:                 var tempTextBox = document.getElementById("number").value;
   4:                 
   5:                 if(tempTextBox>10)
   6:                 {
   7:                     alert("您输入的数字大于10");
   8:                 }
   9:                 else
  10:                 {
  11:                     alert("您输入的数字小于10");
  12:                 }
  13:             }
  14:         
</script>
   7:     </head>
   8:     <body>
   9:         <form id="testform">
  10:             <input type="text" id="number" value="">
  11:             <input type="button" value="判断" onclick="testIF()">
  12:         </form>
  13:     </body>
  14: </html>

for语句的例子:

   1: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
   2: <html>
   3:     <head>
   4:         <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   5:         <title>Untitled Document</title>
   6:         <script type="text/javascript">
   1:  
   2:             function testFor()
   3:             {
   4:                 var tempValue=document.getElementById("testFor").value;
   5:                 for(i=0;i<tempValue;i++)
   6:                 {
   7:                     alert(i);
   8:                 }
   9:             }
  10:         
</script>
   7:     </head>
   8:     <body>
   9:         <input type="text" id="testFor">
  10:         <input type="button" id="demoid" value="点击" onclick="testFor()">
  11:     </body>
  12: </html>

在javascript中我们也支持面对对象,只不过是模拟出来的。

   1: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
   2: <html>
   3:     <head>
   4:         <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   5:         <title>Untitled Document</title>
   6:         <script type="text/javascript">
   1:  
   2:  
   3:                 var Person=new Object();
   4:                 function sum(a,b)
   5:                 {
   6:                     return a+b;
   7:                 }
   8:                 
   9:                 Person.sum=sum;
  10:                 //sum是一个属性还是一个方法要看后面跟的是一个什么
  11:                 alert(Person.sum(3,5));
  12:                 //可以使用匿名函数的方式来实现方法的声明
  13:                 Person.age=function(){return 8+8;};
  14:                 alert(Person.age());
  15:                 //带参数的匿名函数
  16:                 Person.test=function(a,b){return a+b};
  17:                 alert(Person.test(4,6));
  18:                 
  19:                 //以下方式是为了模拟属性的实现
  20:                 Person.Age=20;
  21:                 Person.setAge=function(a){Person.Age=a};
  22:                 Person.setAge(555);
  23:                 alert(Person.Age);
  24:         
</script>
   7:     </head>
   8:     <body >
   9:     </body>
  10: </html>

 

posted @   楚广明  阅读(682)  评论(2编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示