Javascript基础知识(一)核心基础语法与事件模型

  一.Javascript核心基础语法

  1.Javascript是一门增加交互效果的编程语言,它最初由网景公司发明,最后提交给ECMA(欧洲计算机制造商协会),ECMA将Javascript标准化,其命名为Javascript。

  2.Javascript是一门解释性语言,无需编译就可以直接在浏览器下运行。

  3.Javascript的用途?

  1.可以控制网页中所有元素,增加.删除.修改元素的属性。

  2.可以在html中放入动态文本。

  3.响应用户在使用网页时产生的事件。

  4.校验用户输入的数据。

  5.检测用户的浏览器。

  6.用于创建cookie。

  4.Javascript在html网页中创建的三种方式

  1.外部样式:

  创建一个文件名为:xx.js的文件通过<script src="xx.js"><script>来链接

  2.内嵌样式:

  在html中head或body里使用<script type="text/javascript"></script>或直接使用<script></script>载入

  3.内联样式:

  直接在标签中添加事件:<input onclick="alert('helloworld!')">载入

  5.Javascript的数据类型:

  它的数据类型有两大类:1.原始性数据类型2.引用性数据类型(对象)

  原始性数据类型:1.typeof 2.number 3.string 4.boolean 5.null 6.undefined

  引用性数据类型:(预定义的对象有三种)1.原生对象(Object,number,string,boolean,function,Array,Date等)2.内置对象:不需要显示初始化(math,Global)3.宿主对象(主要有BOM和DOM)

  6.BOM和DOM

  BOM:浏览器对象模型Browser Object Model

  DOM:文档对象模型Document Object Model

  二.Javascript的事件模型

  1.Javascript事件模型:1.冒泡类型: <input type="button">当用户点击按钮时:input-body-html-document-window(从下往上冒泡)IE浏览器只是用冒泡

  2.捕获类型: <input type="button">当用户点击按钮时:window-document-html-body-input (从上往下)

  经过ECMA标准化后,其他浏览器都支持两种类型,捕获先发生。

  2.传统事件书写的三种方式:

  1.<input type="button" onclick="alert('helloworld!')">

  2.<input type="button onclick=name1()">======<script>function name1(){alert('helloword!');}</script> //有名函数

  3.<input type="button" id="input1">  //匿名函数

  

复制代码 代码如下:

  <script>

  Var button1=document.getElementById("input1");

  button1.onclick=funtion(){

  alert('helloword!')

  }

  </script>

  3.现代事件书写方式:

  

复制代码 代码如下:

  <input type="button" id="input1">  //IE中添加事件

  <script>

  var fnclick(){

  alert("我被点击了")

  }

  var Oinput=document.getElementById("input1");

  Oinput.attachEvent("onclick",fnclick);

  --------------------------------------

  Oinput.detachEvent("onclick",fnclick);//IE中删除事件

  </script>

  <input type="button" id="input1">  //DOM中添加事件

  <script>

  var fnclick(){

  alert("我被点击了")

  }

  var Oinput=document.getElementById("input1");

  Oinput.addEventListener("onclick",fnclick,true);

  --------------------------------------

  Oinput.removeEventListener("onclick",fnclick);//DOM中删除事件

  </script>

  <input type="button" id="input1"> //兼容IE和DOM添加事件

  <script>

  var fnclick1=function(){alert("我被点击了")}

  var fnclick2=function(){alert("我被点击了")}

  var Oinput=document.getElementById("input1");

  if(document.attachEvent){

  Oinput.attachEvent("onclick",fnclick1)

  Oinput.attachEvent("onclick",fnclick2)

  }

  else(document.addEventListener){

  Oinput.addEventListener("click",fnclick1,true)

  Oinput.addEventListener("click",fnclick2,true)

  }

  </script>