JavaScript——基本语法

单词掌握

  BOM 浏览器对象模型    DOM 文档对象模型    document 文档

  break 中断         continue 继续

1.js脚本位置

  通常可以在三个地方编写js脚本代码,一是在网页文件的<script></script>标签对中直接编写,二是将js代码放置在一个单独的文件中(.js后缀文件),三是将脚本程序代码作为某个HTML元素的事件属性或超链接的href属性值。

1.1 放置在<script></script>标签对中,可以放在文档的任何位置(如:<head>标签,<body>标签中)

1 <body>
2     <script>
3         document.write("hello word");
4     </script>
5 </body>
View Code

1.2 放置在js文件中(在js文件中无需使用<script>标签)

 1 /**index.js**/

2 document.write("hello word"); 

  在html页面中引入js文件(引入的script标签中不能在写js代码)

<script src="index.js" type="text/javascript"></script>

1.3 将js代码作为属性值

  <a href="javascript:document.write('<h2>hello word</h2>')"></a> 

1.4 事件中

扫描二维码关注公众号,回复: 3313933 查看本文章

  <input type="button" value="点击" onclick="alert('点击')" /> 

2 基础语法

2.1 注释

   js注释: //当行注释 /*多行 注释*/              HTML注释: <!--单行注释--> <!--对行 注释--> 

2.2 变量(js是弱类型语言,声明是无需声明类型)  

1 var index=1;
2 ar money=0.5;
3 var chars='a';
4 var name="李白";
5 var isor=true;

2.3 typeof操作符:检测变量的数据类型

  document.write(typeof(index)); 

2.4 运算符

js中运算符
类型 运算符
算术运算符 + - * / % ++ --
赋值运算符 =
比较运算符 > < >= <= == !=
逻辑运算符 && || !

  

   

  pat: " + "运算符也是字符串拼接符,可用于链接字符串

   "李"+"白" == "李白" 

猜你喜欢

转载自www.cnblogs.com/weiyongguang/p/9690521.html