Home  >  Article  >  WeChat Applet  >  Detailed explanation of WeChat applet components: label label

Detailed explanation of WeChat applet components: label label

高洛峰
高洛峰Original
2017-03-27 11:32:271947browse

label labelComponent description:

label tag is very similar to html label. The label element does not present any special effects to the user. However, it improves usability for mouse users. This control is triggered if you click on the text inside the label element. That is to say, when the user selects the label, the focus will automatically go to the form control bound to the label, which is mainly used to improve the usability of the form component.
Use the for attribute to find the corresponding id, or place the control under the label. When clicked, the corresponding control will be triggered.
for has a higher priority than internal controls. When there are multiple controls inside, the first control is triggered by default.
Currently the controls that can be bound are: de935b9c153fbe5d6ad7df96bd65d7b8, a2f269297782d34318beedfba321faa2, dd7cdcbe81dd3b31defaf1177bb27d90, 698d939a2c9041f2302734cfeb04788e


##label label ComponentThe sample code runs as follows:

详解微信小程序组件:label标签

##The following is WXML Code:

[/p][color=rgb(51, 51, 51)][font=&quot;][size=13px]<view class="content">
  <text class="section__title">-------label绑定checkbox(内嵌)-------</text>
  <checkbox-group bindchange="checkboxChange">
    <view class="label-1" wx:for="{{checkboxItems}}">
      <label>
        <checkbox hidden value="{{item.name}}" checked="{{item.checked}}"></checkbox>
        <view class="label-1__icon">
          <view class="label-1__icon-checked" style="opacity:{{item.checked ? 1: 0}}"></view>
        </view>
        <text class="label-1__text">{{item.value}}</text>
      </label>
    </view>
  </checkbox-group>
</view>
<view class="content">
  <text class="section__title">---------label绑定radio(for)---------</text>
  <radio-group class="group" bindchange="radioChange">
    <view class="label-2" wx:for="{{radioItems}}">
      <radio id="{{item.name}}" hidden value="{{item.name}}" checked="{{item.checked}}"></radio>
      <view class="label-2__icon">
        <view class="label-2__icon-checked" style="opacity:{{item.checked ? 1: 0}}"></view>
      </view>
      <label class="label-2__text" for="{{item.name}}"><text>{{item.name}}</text></label>
    </view>
  </radio-group>
</view>
<view class="content">
  <text class="section__title">-----------label绑定button-----------</text>
  <label for="buttontest">label绑定button(for)</label>
  <button id="buttontest" bindtap="testLabelBindButton_1">Fly-1</button>
  <label>
    <text>label绑定button(内嵌)</text>
    <button bindtap="testLabelBindButton_2">Fly-2</button>
  </label>
</view>
<view class="content">
  <text class="section__title">-----------label绑定switch-----------</text>
  <view>
    <label for="switchtest">label绑定switch( for)</label>
    <switch id="switchtest" checked/>
  </view>
  <view>
    <label>
      <text>label绑定switch(内嵌)</text>
      <switch/>
    </label>
  </view>
    <view>
    <label>
      <text>label绑定switch(内嵌)</text>
      <switch/>
      <switch/>
      <switch/>
    </label>
  </view>
</view>

The following is the JS code:

##

Page({
  data: {
    checkboxItems: [
      {name: &#39;ctrip&#39;, value: &#39;携程&#39;, checked: &#39;true&#39;},
      {name: &#39;qunar&#39;, value: &#39;去哪儿&#39;},
      {name: &#39;tuniu&#39;, value: &#39;途牛&#39;}
    ],
    radioItems: [
      {name: &#39;ctrip&#39;, value: &#39;携程&#39;},
      {name: &#39;qunar&#39;, value: &#39;去哪儿&#39;, checked: &#39;true&#39;},
      {name: &#39;tuniu&#39;, value: &#39;途牛&#39;}
    ],
    hidden: false
  },
  checkboxChange: function(e) {
    var checked = e.detail.value
    var changed = {}
    for (var i = 0; i < this.data.checkboxItems.length; i ++) {
      if (checked.indexOf(this.data.checkboxItems.name) !== -1) {
        changed[&#39;checkboxItems[&#39;+i+&#39;].checked&#39;] = true
      } else {
        changed[&#39;checkboxItems[&#39;+i+&#39;].checked&#39;] = false
      }
    }
    this.setData(changed)
  },
  radioChange: function(e) {
    var checked = e.detail.value
    var changed = {}
    for (var i = 0; i < this.data.radioItems.length; i ++) {
      if (checked.indexOf(this.data.radioItems.name) !== -1) {
        changed[&#39;radioItems[&#39;+i+&#39;].checked&#39;] = true
      } else {
        changed[&#39;radioItems[&#39;+i+&#39;].checked&#39;] = false
      }
    }
    this.setData(changed)
  },
  testLabelBindButton_1:function(){
    console.log("奔走相告,button通过for可以绑定成功啦!!!");
  },
  testLabelBindButton_2:function(){
    console.log("奔走相告,button通过内嵌可以绑定成功啦!!!");
  }
})


The following is the WXSS code:

##

.label-1, .label-2{
    margin-bottom: 15px;
}
.label-1__text, .label-2__text {
    display: inline-block;
    vertical-align: middle;
}
.label-1__icon {
    position: relative;
    margin-right: 10px;
    display: inline-block;
    vertical-align: middle;
    width: 18px;
    height: 18px;
    background: #CAE1FF;
}
.label-1__icon-checked {
    position: absolute;
    top: 3px;
    left: 3px;
    width: 12px;
    height: 12px;
    background: #1aad19;
}
.label-2__icon {
    position: relative;
    display: inline-block;
    vertical-align: middle;
    margin-right: 10px;
    width: 18px;
    height: 18px;
    background: #CAFF70;
    border-radius: 50px;
}
.label-2__icon-checked {
    position: absolute;
    left: 3px;
    top: 3px;
    width: 12px;
    height: 12px;
    background: #1aad19;
    border-radius: 50%;
}
.section__title{
  display: block;
  text-align: center;
  color: #9400D3;
}
.content{
  padding-bottom: 15px;
}

Main attributes:


##Attributes

TypeTypeThe id of the bound control (the id will only take effect if it is exactly the same as the id of the form control that needs to be bound)

for

String

The above is the detailed content of Detailed explanation of WeChat applet components: label label. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn