好程式設計師web前端教程分享前端 javascript 練習題二

好程式設計師IT發表於2019-10-11

   好程式設計師web前端 教程 分享 前端 javascript 練習題 建立表格,新增刪除操作
介紹:這是用基本的方式,更便捷的方法是,使用es6提供的模板字串,程式碼和效率能提高很多
簡單的樣式:

a < input type =" text "/>

b < input type =" text "/>

c < input type =" text "/>< input type =" button "  value =" 新增 "/>< table ></ table >

js實現程式碼:

var  oinput = document . getElementsByTagName ( "input" ); var  otable = document . getElementsByTagName ( "table" )[ 0 ];

oinput [ oinput . length - 1 ]. onclick = function (){

     var  otr = document . createElement ( "tr" );

     //建立單元格,並且將文字的值加到單元格里

     for ( var  i = 0 ; i < oinput . length - 1 ; i ++ ){

         var  otd = document . createElement ( "td" );

        otd . innerHTML = oinput [ i ]. value ;

        otr . appendChild ( otd );

     }

     //新增刪除按鈕,並加入到單元格中

     var  otd =  document . createElement ( "td" );

         var  oSpan =  document . createElement ( "span" );

        oSpan . innerHTML =   "刪除" ;

        otd . appendChild ( oSpan );

        otr . appendChild ( otd );

        oSpan . onclick   =   function (){

             //oTable.removeChild(oTr);

            otable . removeChild ( this . parentNode . parentNode );

         }

    otable . appendChild ( otr );}

進度條
html排版樣式:

< div class =" progressBar ">

    < p style =""></ p ></ div >< div class =" progressBar ">

    < p ></ p ></ div >< div class =" progressBar ">

    < p ></ p ></ div >

js執行程式碼:

var  opp = document . getElementsByTagName ( "p" ); function   progress (obj,num){

     var  count = 0 ;

     var  timer = setInterval ( function (){

        count ++ ;

        obj . style . width = count + "%" ;

         if ( count == num ){

             clearInterval ( timer );

         }

     }, 20 )} progress ( opp [ 0 ], 50 ); progress ( opp [ 1 ], 60 ); progress ( opp [ 2 ], 80 );

 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69913892/viewspace-2659561/,如需轉載,請註明出處,否則將追究法律責任。

相關文章