JavaScript内置对象 以及和 内置对象相关的语法

1.this :指当前的对象

 <img src="image/head.jpg" onclick="alert(this.src)">//this指当前这标签对象 即img对象,点击这个图片时,会弹出这个图片的路径
<html>
  <head>
  </head>
  <script type="text/javascript">
   function check(obj){
    if(obj.value==""){
      alert("用户名为空");
    }
    else{
      alert(obj.value);
    }
   }
   
  </script>
  
  <body>
   <img src="image/head.jpg" onclick="alert(this.src)">
   <form action="" method="post">
   <input type="text" name="username" onclick ="return check(this)">   //this指当前对象 input这个标签
   </form>  
  </body>
</html>

 

2.for...in  :in后跟一个对象,对此对象中所有元素循环一次

<html>
  <head>
  </head>
  <script type="text/javascript">
  var arr = new Array("gao","wei","gang");  
  </script> 
  <body>
   <script type="text/javascript">
    document.write(arr.length+"</br>");
     for(eee in arr){       //eee代表数组arr中属性的属性名
        document.write(eee+"--");
        document.write(arr[eee]+"</br>");//取出arr对象中名字为arr的属性的值
     }
   </script>
  </body>
</html>

3.with      :为一段代码建立一个缺省的对象,任何无对象属性的引用,都将使用该缺省的对象

<script type="text/javascript">
     with(document){  //建立一个缺省的对象
        write("ddddddddddd"+"</br>");//使用缺省的对象
        write("ffffffffff"+"</br>");
        write("kkkkkkkkkkk"+"</br>");
    
     }
   </script>

4.new :用于生成一个新对象

 <script type="text/javascript">
      var today = new Date();
      alert(today.getDate());//today.getDate()返回当前 日(几月几日的日)
   </script>

 

 

posted @ 2013-04-13 09:27  springstudent  阅读(232)  评论(0编辑  收藏  举报