div模擬textarea

今天原本呢是要搜js中的createTextRange,結果我輸入createTextRange,按下回車,百度第二條就是張鑫旭前輩的博文,意外的驚喜從我點擊進去的那一刻就發生了,web

驚喜在這裏:HMTL的塊標籤可讓其像textarea同樣支持blur,focus事件;瀏覽器

怎麼作到的呢?就我目前一個小菜鳥的水平,這三個簡單方法就能夠了:blog

    第一個方法:contenteditable="true"   事件

      <p contenteditable="true" style="width: 300px ;height: 300px; border:1px solid #f66;"></p>開發

      contenteditable的屬性嘛,知道:true,false,再加plaintext-only就夠了it

      contenteditable=「」event

      contenteditable=「true」table

      contenteditable=「false」百度

      contenteditable=「plaintext-only」webkit

      contenteditable=「events」

      contenteditable=「caret」

      這個方法有個缺陷:假如是經過複製黏貼過來的內容,不會去格式化,這就是個尷尬的事了,以下:

      

     這種方式沒有什麼兼容問題,我試過的Chrome,IE,Firefox,Opera都支持,那還有什麼更好的方式解決這個問題呢?

    第二個方法:其實它的屬性值裏已經有了,哈哈哈,contenteditable=「plaintext-only」,又被你發現了,

      

      完美解決,但,遺憾的是隻有Chrome和Opera支持,我測的Opera版本:

    第三個方法:user-modify

      user-modify:read-only

      user-modify:write-only

      user-modify:read-write

      user-modify:read-write-plaintext-only(去格式化)

      開發中的時候要加上瀏覽器的前綴:

      -webkit-user-modify: read-write-plaintext-only;(支持去格式化)

      

      -webkit-user-modify: read-write;(不支持去格式化)

      

      一樣的,這種方式也只有Chrome和Opera支持;

相關文章
相關標籤/搜索