移動端的坑 之 在可編輯的div中實現placeholder

來源:互聯網
上載者:User

標籤:please   ipo   lease   編輯   內容   hold   文字框   沒有   換行   

1.當我們個  div 設定了 contenteditable = "true" 這個塊狀元素就可以編輯了, 如果要實現 placeholder 直接寫上去是沒有效果的

<div contenteditable="true"  placeholder="please write infomation"></div>//沒有效果

 

2.可以添加樣式來實現

*[contenteditable="true"]:empty:before{        content:attr(placeholder);        color: #A9A9A9;    }

這段代碼匹配 屬性 contenteditable="true" 的元素類容為empty時添加:before虛擬元素 

  attr(placeholder)擷取屬性placeholder的值賦值給content這樣就可以類比的實現文字框中 placeholder 屬性,

 

3.在ipone機中首次顯示時是ok的,當你輸入內容後再清空發現 並沒有顯示再顯示placeholder的內容,這是因為這個可編輯的塊這時不是空,還有分行符號,

如果我們有使用jquery,zepto等我們可以拿到這個元素的text();跟這個元素再賦值“”;

如果我們用js,用innerText去擷取塊元素的值,你會發現不管你值是否為空白都是true,就沒有辦法判斷我什麼時候是空(只有在ipone中才這樣),

然後我就去看了下jquery中的text()是怎麼實現的,它用了一個textContent屬性,用這個屬性就可以判斷到

移動端的坑 之 在可編輯的div中實現placeholder

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.