基於javascript製作微博發布欄效果_javascript技巧

來源:互聯網
上載者:User

本文為大家分享了做微博發布欄效果的過程,涉及到的知識點包括以下:

1.判斷IE的方法:直接用  var ie=!-[1,];即可

2.連續發生事件的用法:

 IE下:觸發對象.onpropertychange

 標準下:觸發對象.oninput

3.焦點聚集和移開事件。onfocus和onblur

4.判斷單位元組(0-255之間)與雙子節:Regex:/[^\x00-\xff]/g

代碼如上:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><style>#div1{width: 400px;margin: 20px auto;border: 1px solid #ccc}#div1 p{float: right;margin: 0;font-size: 13px;}#div1 textarea{width: 400px;height: 280px;}#div1 a{background: #ccc;float: right;color: #FFFFFF;text-align: center;background: #00FF00;width: 50px;height: 30px}#div1 a.dis{background: #ccc;color: black;}</style><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title><script type="text/javascript">window.onload=function (){    var oDiv=document.getElementById('div1');    var oP=oDiv.getElementsByTagName('p')[0];    var oT=oDiv.getElementsByTagName('textarea')[0];    var oA=oDiv.getElementsByTagName('a')[0];    var bool=true;    var ie=!-[1,];    var timer=null;    var num=0;    //給文字框加聚焦事件    oT.onfocus=function()    {     if(bool)     {      oP.innerHTML='你還可以輸入<span>90</span>字';      bool=false;     }     }     oT.onblur=function()    {     if(oT.value=='')     {      oP.innerHTML='請輸入你的留言';      bool=true;     }     }    //輸入內容,計算字數    if(ie)    {     oT.onpropertychange=toChange;//連續觸發    }    else    {    oT.oninput=toChange;    }    function toChange()    {       var num=Math.ceil(getLength(oT.value)/2);//向上取整       var oSpan=oDiv.getElementsByTagName('span')[0];       if(num<=90)       {        oSpan.innerHTML=90-num;        oSpan.style.color='';       }      else      {        oSpan.innerHTML=num-90;        oSpan.style.color='red';      }      if(oT.value==''||num>90)      {      oA.className='dis';      }      else      {       oA.className='';      }    }    function getLength(str)    {    return String(str).replace(/[^\x00-\xff]/,'aa').length;//不是單雙節的將其變為兩個單雙節的    }    //點擊按鈕,變色    oA.onclick=function()    {      if(this.className=='dis')      {        clearInterval(timer);        timer=setInterval(function(){          if(num>5){clearInterval(timer);num=0;}          else{            num++;          }          if(num%2)          {            oT.style.background='red';          }          else          {             oT.style.background='';          }        },100)      }      else      {        alert('發布成功');      }    }    }</script></head><body ><div id='div1'> <p>請輸入你的留言</p> <textarea></textarea> <a href="#" class="dis">發布</a></div>  </body></html>

以上就是本文的全部內容,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.