微信小程序教程-按钮组件例子

https://developers.weixin.qq.com/miniprogram/dev/component/button.html

 

示例wxml

<button type="default" size="{{defaultSize}}" loading="{{loading}}" plain="{{plain}}"
        disabled="{{disabled}}" bindtap="default" hover-class="other-button-hover"> default </button>
<button type="primary" size="{{primarySize}}" loading="{{loading}}" plain="{{plain}}"
        disabled="{{disabled}}" bindtap="primary"> primary </button>
<button type="warn" size="{{warnSize}}" loading="{{loading}}" plain="{{plain}}"
        disabled="{{disabled}}" bindtap="warn"> warn </button>
<button bindtap="setDisabled">点击设置以上按钮disabled属性</button>
<button bindtap="setPlain">点击设置以上按钮plain属性</button>
<button bindtap="setLoading">点击设置以上按钮loading属性</button>
<button open-type="contact">进入客服会话</button>
<button open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="onGotUserInfo">获取用户信息</button>

示例js

var types = [‘default‘, ‘primary‘, ‘warn‘]
var pageObject = {
  data: {
    defaultSize: ‘default‘,
    primarySize: ‘default‘,
    warnSize: ‘default‘,
    disabled: false,
    plain: false,
    loading: false
  },
  setDisabled: function(e) {
    this.setData({
      disabled: !this.data.disabled
    })
  },
  setPlain: function(e) {
    this.setData({
      plain: !this.data.plain
    })
  },
  setLoading: function(e) {
    this.setData({
      loading: !this.data.loading
    })
  },
  onGotUserInfo: function(e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.userInfo)
    console.log(e.detail.rawData)
  },
}

for (var i = 0; i < types.length; ++i) {
  (function(type) {
    pageObject[type] = function(e) {
      var key = type + ‘Size‘
      var changedData = {}
      changedData[key] =
        this.data[key] === ‘default‘ ? ‘mini‘ : ‘default‘
      this.setData(changedData)
    }
  })(types[i])
}

Page(pageObject)

我的js

Page({
  data: {
    defaultSize: ‘default‘,
    warnSize:‘default‘,
    primarySize: ‘default‘,

    loading: false,
    plain: false,
    disabled: false,
  },

  default: function(){
    this.setData({defaultSize: (this.data.defaultSize == ‘default‘) ? ‘mini‘ : ‘default‘});
  },
  warn: function () {
    this.setData({ warnSize: (this.data.warnSize == ‘default‘) ? ‘mini‘ : ‘default‘ });
  },
  primary: function () {
    this.setData({ primarySize: (this.data.primarySize == ‘default‘) ? ‘mini‘ : ‘default‘ });
  },

  setDisabled: function(){
    this.setData({disabled: this.data.disabled==false?true:false});
  },
  setPlain: function () {
    this.setData({ plain: this.data.plain == false ? true : false });
  },
  setLoading: function () {
    this.setData({ loading: this.data.loading == false ? true : false });
  }
})

 

微信小程序教程-按钮组件例子

上一篇:微信小程序


下一篇:值得收藏的CSS小技巧