溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

微信小程序中怎么實(shí)現(xiàn)事件交互操作

發(fā)布時(shí)間:2021-06-16 17:01:59 來(lái)源:億速云 閱讀:261 作者:Leah 欄目:web開(kāi)發(fā)

本篇文章給大家分享的是有關(guān)微信小程序中怎么實(shí)現(xiàn)事件交互操作,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

什么是事件?

指點(diǎn)擊,觸摸,按下,滑動(dòng),松開(kāi),等一系列對(duì)手機(jī)屏幕操作。

下面代碼所要呈現(xiàn)的效果就是給兩個(gè)按鈕一人一個(gè)ID然后點(diǎn)擊誰(shuí),在上面顯示信息那就顯示 點(diǎn)擊了誰(shuí),點(diǎn)擊了多少次,這多少次沒(méi)有分開(kāi)啊,次數(shù)是點(diǎn)擊他兩的總和。下面另一代碼是分開(kāi)的,各計(jì)各的。

微信小程序中怎么實(shí)現(xiàn)事件交互操作

1. 在index.wxml 中設(shè)置點(diǎn)擊事件(測(cè)試時(shí)需要?jiǎng)h除注釋部分)

<view class="page">
    //點(diǎn)擊后在這里顯示信息,來(lái)表明是有點(diǎn)擊事件的
   <view>{{clickMsg}}</view>
//設(shè)置點(diǎn)擊事件
<view>
  <view id="1" class="view-item" bindtap="clickMe">按鈕1</view>
  <view id="2" class="view-item" bindtap="clickMe">按鈕2</view>
</view>
</view>

2. 在index.wxss中設(shè)置view的樣式

.page{
text-align: center;
}
.view-item{
background-color:green;
width: 60px;
height: 30px;
margin: 30px auto 0 auto;
color: navajowhite;
border-radius: 10px;
padding: 20px;
}

3. 在index.js中設(shè)置點(diǎn)擊事件的響應(yīng)

//注冊(cè)兩個(gè)變量
var id;
var count = 0;
var param = {
 data: {
  clickMsg: '顯示點(diǎn)擊內(nèi)容'
 },
 //function就是觸發(fā)點(diǎn)擊事件的函數(shù),以后是點(diǎn)擊事件你就這樣寫(xiě)就行
 //e就是event事件對(duì)象,包含了很多,比如:誰(shuí)被點(diǎn)擊了,什么時(shí)候被點(diǎn)擊了
 clickMe: function (e) {
  count++;
  console.log(e);//把點(diǎn)擊事件詳細(xì)信息打印到調(diào)試的console中
  id = e.currentTarget.id; //獲取被點(diǎn)擊按鈕的id
  param.data.clickMsg = '顯示點(diǎn)了誰(shuí):' + id + '點(diǎn)擊次數(shù)' + count;
  //重新刷新clickMsg的顯示內(nèi)容,要想看到message變化必須寫(xiě)這個(gè)
  this.setData(param.data);
 }
};
Page(param);
//這個(gè)必須寫(xiě),它是為了讓param這個(gè)函數(shù)真實(shí)化,要不然你干寫(xiě)著,在index.wxml中調(diào)用時(shí)沒(méi)反應(yīng)的

怎樣讓點(diǎn)擊控件view0攜帶私有信息呢

為了以后傳值什么的

<view class="view-item" data-siyou="飛了" bindtap="clickMe">點(diǎn)擊我view0</view>

怎樣調(diào)用控件私有信息呢?

var param={
  clickMe:function(e){
     id=e.currentTarget.id;
     console.log(e.currentTarget.id.dataset.siyou);
}
};
Page(param);

二、添加判斷的點(diǎn)擊事件

微信小程序中怎么實(shí)現(xiàn)事件交互操作

index.wxml

<view class="page">
   <view>{{Message}}</view>
   <view>{{Message1}}</view>
<view>
  <view id="view1" class="view-item" bindtap="clickMe">按鈕1</view>
  <view id="view2" class="view-item" bindtap="clickMe">按鈕2</view>
</view>
</view>

index.js

var id;
var count1 = 0;
var count2 = 0;
var param = {
 data: {
  Message: '點(diǎn)擊后,在這里顯示信息',
  Message1: '點(diǎn)擊后,在這里顯示信息'
 },
 clickMe: function (e) {
  console.log(e);
  id = e.currentTarget.id;
  if (id == 'view1') {
   count1++;
   param.data.Message = '顯示點(diǎn)擊了' + id + '點(diǎn)擊了' + count1 + '次';
   this.setData(param.data);
  } else if (id == 'view2') {
   count2++;
   param.data.Message1 = '顯示點(diǎn)擊了' + id + '點(diǎn)擊了' + count2 + '次';
   this.setData(param.data);
  }
 }
};
Page(param);

以上就是微信小程序中怎么實(shí)現(xiàn)事件交互操作,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI