【轉】如何?<textarea> placeholder自動換行?

來源:互聯網
上載者:User

標籤:

轉自:http://segmentfault.com/q/1010000002677808

 

寫的是移動端的web,定義了一個textarea,在placeholder中添加了一些提示。由於有些手機螢幕不同,placeholder的內容不會自動換行,而是超出了螢幕顯示地區。
之前搜尋過一些關於placeholder換行的內容,說是加入ward="hard"屬性強制換行(添加過,無效。)手動設標記換行(對於其他螢幕大行動裝置不合適了)。
請問怎麼樣可以讓placeholder的內容可以自動換行?
(ps:在電腦瀏覽器中和iPhone上會自動換行,但在Android內則無法實現。很奇怪的情況)

 

以前的博文,直接 COPY 上來了。

在 HTML5 placeholder 中,意為預留位置。常會在表單中用一些簡單的單詞進行提示,友好的提示使用者錄入資料。

在 W3C 中,定義佔位符為一個簡單的提示(一個詞語或一個短語),在協助使用者進行資料錄入。若想錄入較長的提示,建議在當前操作旁註明提示資訊,而不是使用 placeholder。

The placeholder attribute represents a short hint (a word or short phrase) intended to aid the user with data entry. A hint could be a sample value or a brief description of the expected format. The attribute, if specified, must have a value that contains no U+000A LINE FEED (LF) or U+000D CARRIAGE RETURN (CR) characters.

For a longer hint or other advisory text, place the text next to the control.

來源:http://www.w3.org/html/wg/drafts/html/master/forms.html#the-placeholde...

倘若硬是想在 textarea 標籤中使用 placeholder 屬性去實現換行文字提示的話,有什麼辦法實現呢?

於是有了以下嘗試:

直接添加各種分行符號是不可行的

一開始我以為,直接加上分行符號就能實現,於是乎有了下面的結果:

<textarea placeholder="單行文本提示" rows="3"></textarea><textarea placeholder="第一行文本提示 \n 第二行文本提示 <br/> 第三行文本提示 \A 第四行文本提示" rows="3"></textarea>

<iframe width="100%" height="120" src="http://jsfiddle.net/samzeng/G276g/embedded/result/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>

萬能的 CSS方法一
/* WebKit browsers */::-webkit-input-placeholder {    color: transparent;}::-webkit-input-placeholder:before { display: block; color: #999; content: "第一行文本提示 \A 第二行文本提示 \A 第三行文本提示 \A";}/* Mozilla Firefox 4 to 18 */:-moz-placeholder { color: transparent;}:-moz-placeholder:before { display: block; color: #999; content: "第一行文本提示 \A 第二行文本提示 \A 第三行文本提示 \A";}/* Mozilla Firefox 19+ */::-moz-placeholder { color: transparent;}::-moz-placeholder:before { display: block; color: #999; content: "第一行文本提示 \A 第二行文本提示 \A 第三行文本提示 \A";}/* Internet Explorer 10+ */:-ms-input-placeholder { color: transparent;}:-ms-input-placeholder:before { display: block; color: #999; content: "第一行文本提示 \A 第二行文本提示 \A 第三行文本提示 \A";}

<iframe width="100%" height="120" src="http://jsfiddle.net/samzeng/2phe5/embedded/result/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>

方法二
* WebKit browsers */::-webkit-input-placeholder:after { display: block; content: "第二行文本提示 \A 第三行文本提示 \A";}/* Mozilla Firefox 4 to 18 */:-moz-placeholder:after { display: block; content: "第二行文本提示 \A 第三行文本提示 \A";}/* Mozilla Firefox 19+ */::-moz-placeholder:after { display: block; content: "第二行文本提示 \A 第三行文本提示 \A";}/* Internet Explorer 10+ */:-ms-input-placeholder:after { display: block; content: "第二行文本提示 \A 第三行文本提示 \A";}

<iframe width="100%" height="120" src="http://jsfiddle.net/samzeng/s42hj/embedded/result/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>

萬能的 JavaScript
// required jQueryvar placeholder = ‘第一行文本提示\n第二行文本提示\n第三行文本提示‘;$(‘textarea‘).val(placeholder);$(‘textarea‘).focus(function() { if ($(this).val() == placeholder) { $(this).val(‘‘); }});$(‘textarea‘).blur(function() { if ($(this).val() == ‘‘) { $(this).val(placeholder); }});

<iframe width="100%" height="120" src="http://jsfiddle.net/samzeng/vrGXa/embedded/result/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>

【轉】如何?<textarea> 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.