Web基础之Javascript 进阶

Javascript 进阶

动态添加表格内容:

<script type="text/javascript">

      var length= 0; //只初始化一次

      function addrow(){

         var name= document.getElementById("name").value;

         var rank= document.getElementById("rank").value;

         var nickname = document.getElementById("nickname").value;

         // varlength = document.getElementById("t1").rows[].length;

         var newrow = document.getElementById("t1").insertRow(++length);

         var x =newrow.insertCell(0);

         var y =newrow.insertCell(1);

         var z =newrow.insertCell(2);

         x.innerHTML= name;

         y.innerHTML= rank;

         z.innerHTML= nickname;

         document.getElementById("name").value= "";

         document.getElementById("rank").value= "";

         document.getElementById("nickname").value= "";

      }

   </script>

<body>

    <tableid="t1">

      <tr>

         <td>姓名</td><td>排名</td><td>绰号</td>

      </tr>

   </table>

    <div>

      姓名:<input type="text"id="name"><br/>

      排名:<input type="text"id="rank"><br/>

      绰号:<input type="text"id="nickname"><br/>

      <input type="button"οnclick="addrow()" value="添加">

   </div>   

  </body>


父窗口与子窗口通信:

Parent:

<script type="text/javascript">

      var newwindow;

      function cpen(){                            //JavaScript函数名一定要注意,不能用open()等关键字

         newwindow= window.open("child.html","_blank");

      }

      function call(){

         varcontent = $("parent").value;

         newwindow.document.getElementById("child").value= content;

      }

      function $(id){

         return document.getElementById(id);

      }

   </script>

  <body>

    This is parent <input type="text" id="parent"/>

    <inputtype="button" value="Open" οnclick="cpen()">

  <input type="button"value="call" οnclick="call()"> 

  </body>

Child :

<script type="text/javascript">

      function callback(){

         opener.document.getElementById("parent").value= document.getElementById("child").value;

      }

   </script>

  <body>

    This ischild.<input type="text" id="child"><inputtype="button" value="callback"οnclick="callback()">

  </body>

动态创建和删除标签:

<script type="text/javascript">

      function add1(){

         var new_link = document.createElement("a");

         new_link.href= "http://www.baidu.com";

         new_link.innerText= "百度一下";

         new_link.style.top= "200px";

         new_link.style.left= "300px";

         new_link.style.position= "absolute";

         document.body.appendChild(new_link);

      }

     

      var buttonid = 0;

     

      function add2(){

         var new_button = document.createElement("input");

         new_button.type= "button";

         new_button.value= "I am button";

         new_button.id= ++buttonid;

         document.getElementById("div1").appendChild(new_button);     

      }

     

      function del(){

        document.getElementById("div1").removeChild(document.getElementById(buttonid--));

      }

     

      function shownode(){

         var div1= document.getElementById("div1");

         window.alert(div1.nodeName+""+div1.nodeType);

         window.alert(div1.parentNode.parentNode);

      }

     

      function inHtml(){

         document.getElementById("sp1").innerHTML= "<a href='http://www.sohu.com'>BaiDu</a>";

      }

     

      function inText(){

         document.getElementById("sp1").innerText= "<a href='http://www.sohu.com'>";

      }

   </script>

  <body>

    <input type="button" value="动态添加链接标签" οnclick="add1()"><br>

    <input type="button" value="添加button到div"οnclick="add2()"><br>

   <input type="button" value="动态删除button元素"οnclick="del()">

   <input type="button" value="Show Node"οnclick="shownode()">     

    <divid="div1" style="width:200px;height:150px;border:1px solidred;"></div>

    <span id="sp1"></span>

    <input type="button" value="innerHTML"οnclick="inHtml()">

<input type="button"value="innerText" οnclick="inText()">  </body>


标签内容的显示和隐藏:

<script type="text/javascript">

      function op(e){

         var div1= document.getElementById("div1");

         if (e.id== "hide"){

            div1.style.display= "none";  // 不影响页面布局,若用visibility则会影响

         }else if(e.id == "show"){

            div1.style.display= "block";

         }

      }

   </script>

  <body>

         <div id="div1"style="font-size: 12pt;color:red; ">

            gahogahoghaooaghg<br>

            ahgoahq9hthgagabg<br>

            algagoahogahohago<br>

         </div>

         <input type="button"id="show" οnclick="op(this)" value="Show">

         <input type="button"id="hide" οnclick="op(this)" value="Hide">

  </body>

document.getElementsByName:

function show(){

         var hobbies = document.getElementsByName("hobby");

         window.alert(hobbies.length);

         for(var i=0; i<hobbies.length; i++){      //不能用int

            if(hobbies[i].checked){

                alert(hobbies[i].value);

            }

         }

      }

      function test(){

         var input = document.getElementsByTagName("input");

      }

<body>

      <input type="checkbox"value="football" name="hobby">FootBall

      <input type="checkbox"value="basketball" name="hobby"/>Basketball

      <input type="checkbox"value="voliball" name="hobby">VoliBall

      <input type="button" onclick ="show()" value ="show">

  </body>

JavaScript输入校验:

<script type="text/javascript">

      function check(){

         document.getElementById("sp1").innerText= "";

         document.getElementById("sp2").innerText= "";

         document.getElementById("sp3").innerText= "";

         var check = false;


         if(document.forms[0].username.value.length < 4 ||document.forms[0].username.value.length > 6){

            document.getElementById("sp1").innerText= "用户名长度在4~6之间";

            check= true;

         }

         if(document.forms[0].passwd.value.length < 8){

            document.getElementById("sp2").innerText= "密码长度要大于8";

            check= true;

         }

         if(check){

            check= false;

            return false;  //必须加,否则会提交刷新;

         }

      }

   </script>

<body>

      <form οnsubmit="return check()">

      <table>

         <tr><td>用户名:</td><td><input type="text" name="username"><spanid="sp1"></span></td></tr>

         <tr><td>密码:</td><td><input type="password" name="passwd"><spanid="sp2"></span></td></tr>

         <tr><td>电子邮箱:</td><td><input type="text" name="email"><span id="sp3"></span></td></tr>

         <tr><td><input type="submit" value="提交"style="width:70px"></td><td><input type="reset" value="取消"style="width:70px"></td></tr>

      </table>

      </form>

  </body>

简易时钟(setInterval):

<script type="text/javascript">

      function show(){

         document.getElementById("time").value= new Date().toLocaleString();

      }

      setInterval("show()",1000);     //函数名要加""

   </script>

  <body >

   <input type="text" id="time" maxlength="40"/>

  </body>

桌面弹簧球游戏:

<script type="text/javascript">

      speed = 1;

      directX =5;

      directY =5;

      sumX = 0;

      sumY = 0;

      function move(){

         var rabbit = document.getElementById("rab");

         sumX +=directX;

         sumY +=directY;

         rabbit.style.left= sumX + "px";

         rabbit.style.top= sumY + "px";

        

         if(rabbit.offsetWidth+ sumX >= document.body.clientWidth || sumX <= 0){

            directX= -directX;

         }

        

         if(rabbit.offsetHeight+ sumY >= document.body.clientHeight || sumY <= 0){

            directY= -directY;

         }

      }

      setInterval("move()",speed);

   </script>

<body style="background-color:silver">

    <divid="rab" style="position: absolute;">

      <img src="img/1.jpg"style="width: 100px;"/><br>

      <span style="font-style: italic;color: blue;">Hello,I am baby rabbit!</span>

    </div>

  </body>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值