#### textarea
多行輸入框。
| 屬性名 | 類型 | 默認值 | 說明 | 最低版本 |
| ----------------- | ----------- | -------------------- | ---------------------------------------- | ---------------------------------------- |
| value | String | | 輸入框的內容 | |
| placeholder | String | | 輸入框為空時占位符 | |
| placeholder-style | String | | 指定 placeholder 的樣式 | |
| placeholder-class | String | textarea-placeholder | 指定 placeholder 的樣式類 | |
| disabled | Boolean | false | 是否禁用 | |
| maxlength | Number | 140 | 最大輸入長度,設置為 -1 的時候不限制最大長度 | |
| auto-focus | Boolean | false | 自動聚焦,拉起鍵盤。 | |
| focus | Boolean | false | 獲取焦點 | |
| auto-height | Boolean | false | 是否自動增高,設置auto-height時,style.height不生效 | |
| fixed | Boolean | false | 如果 textarea 是在一個 `position:fixed` 的區域,需要顯示指定屬性 fixed 為 true | |
| cursor-spacing | Number | 0 | 指定光標與鍵盤的距離,單位 px 。取 textarea 距離底部的距離和 cursor-spacing 指定的距離的最小值作為光標與鍵盤的距離 | |
| cursor | Number | | 指定focus時的光標位置 | [1.5.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
| show-confirm-bar | Boolean | true | 是否顯示鍵盤上方帶有”完成“按鈕那一欄 | [1.6.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
| selection-start | Number | -1 | 光標起始位置,自動聚集時有效,需與selection-end搭配使用 | [1.9.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
| selection-end | Number | -1 | 光標結束位置,自動聚集時有效,需與selection-start搭配使用 | [1.9.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
| bindfocus | EventHandle | | 輸入框聚焦時觸發,event.detail = {value: value} | |
| bindblur | EventHandle | | 輸入框失去焦點時觸發,event.detail = {value, cursor} | |
| bindlinechange | EventHandle | | 輸入框行數變化時調用,event.detail = {height: 0, heightRpx: 0, lineCount: 0} | |
| bindinput | EventHandle | | 當鍵盤輸入時,觸發 input 事件,event.detail = {value, cursor},**bindinput 處理函數的返回值并不會反映到 textarea 上** | |
| bindconfirm | EventHandle | | 點擊完成時, 觸發 confirm 事件,event.detail = {value: value} | |
**示例代碼:**
```
<!--textarea.wxml-->
<view class="section">
<textarea bindblur="bindTextAreaBlur" auto-height placeholder="自動變高" />
</view>
<view class="section">
<textarea placeholder="placeholder顏色是紅色的" placeholder-style="color:red;" />
</view>
<view class="section">
<textarea placeholder="這是一個可以自動聚焦的textarea" auto-focus />
</view>
<view class="section">
<textarea placeholder="這個只有在按鈕點擊的時候才聚焦" focus="{{focus}}" />
<view class="btn-area">
<button bindtap="bindButtonTap">使得輸入框獲取焦點</button>
</view>
</view>
<view class="section">
<form bindsubmit="bindFormSubmit">
<textarea placeholder="form 中的 textarea" name="textarea"/>
<button form-type="submit"> 提交 </button>
</form>
</view>
```
```
//textarea.js
Page({
data: {
height: 20,
focus: false
},
bindButtonTap: function() {
this.setData({
focus: true
})
},
bindTextAreaBlur: function(e) {
console.log(e.detail.value)
},
bindFormSubmit: function(e) {
console.log(e.detail.value.textarea)
}
})
```
##### Bug & Tip
1. `bug`: 微信版本 `6.3.30`,`textarea` 在列表渲染時,新增加的 `textarea` 在自動聚焦時的位置計算錯誤。
2. `tip`: `textarea` 的 `blur` 事件會晚于頁面上的 `tap` 事件,如果需要在 `button` 的點擊事件獲取 `textarea`,可以使用 `form` 的 `bindsubmit`。
3. `tip`: 不建議在多行文本上對用戶的輸入進行修改,所以 `textarea` 的 `bindinput` 處理函數并不會將返回值反映到 `textarea` 上。
4. `tip`: `textarea` 組件是由客戶端創建的原生組件,它的層級是最高的,不能通過 z-index 控制層級。
5. `tip`: 請勿在 `scroll-view`、`swiper`、`picker-view`、`movable-view` 中使用 `textarea` 組件。
6. `tip`: `css` 動畫對 `textarea` 組件無效。
- 簡介
- 第一章 公眾號開發
- 使用微信JSSDK
- 接口權限配置
- 分享接口
- 隱藏按鈕項
- 微信支付
- 第二章 小程序開發
- 基礎知識
- 分包加載
- WXSS樣式表
- 配置
- app.json配置
- window
- tabBar
- page.json配置
- 邏輯層
- app.js
- 場景值
- page.js
- 初始化數據
- 生命周期函數
- 頁面相關事件處理函數
- 事件處理函數
- 頁面實例方法
- 路由
- 文件作用域
- 模塊化
- 視圖層
- 模板語法
- 列表渲染
- 條件渲染
- 模板
- 事件
- 引用
- WXS語法規范
- WXS數據類型
- WXS控制流程
- WXS基礎類庫
- 組件
- 視圖容器
- view
- scroll-view
- swiper
- movable-view
- cover-view
- 基礎組件
- icon
- text
- rich-text
- progress
- 表單組件
- button
- checkbox
- form
- input
- label
- picker
- picker-view
- radio
- slider
- switch
- textarea
- 導航組件
- navigator
- 媒體組件
- audio
- image
- video
- camera
- 地圖組件
- map
- 畫布組件
- canvas
- 開放能力
- web-view
- 自定義組件
- 組件模版和樣式
- Component
- 組件傳值
- 組件事件
- Behaviors
- 組件間關系
- 網絡請求
- wx.request
- 微信登錄
- 獲取 openid 和 unionid
- 獲取用戶信息
- 將 wx.request 封裝為 promise
- 上傳圖片接口封裝
- 數據存儲
- 存儲數據和讀取數據
- 獲取數據緩存信息
- 移除數據緩存
- 獲取用戶設置
- openSetting
- getSetting
- 第三章 小游戲開發
- 參考資料