WeChat小程序基础篇-js事件

WeChat小程序基础篇-js事件,第1张

js负责页面与用户的交互,动态修改页面的内容。使用js脚本来处理用户的 *** 作。

*** 作解释: 点击button按钮(点我),修改界面的msg显示为"BeiJing佩奇"

绑定事件: bindtap定义了"点击事件"

j s 函数: Page({定义函数对应bindtap})

例如开头示例一样,组件绑定事件,js编写事件函数。

这里大概介绍下log打印的内容

☞ 小生不才,附上 博客地址☜

事件分为冒泡事件和非冒泡事件

1.冒泡事件:当一个组件被触发后,该事件以此向父节点传递。

2.非冒泡事件:当一个组件触发后,该事件不会向父节点传递。

解释:当点击子节点时,会依次触发"bind事件2,bind事件1"

解释:当点击子节点时,只触发"bind事件2",而没有触发"bind事件1"

自基础班1.5.0起,触摸类事件支持捕获阶段。捕获阶段位于冒泡阶段之前。事件到达节点的顺序与冒泡相反。捕获事件采用的关键字为capture-bind、capture-catch,当然catch还是中断,取消冒泡阶段。

1. 示例1,执行顺序为hand2,hand4,hand3,hand1

2. 示例2,加入capture-catch捕获事件,只执行hand2

小程序的wxs功能可以让wsmxl可以调用和编写js,基本上wxs和JS无关系,只是语法形式很相似。

如下写了两个关于时间的函数,并将它们导出,

<wxs module="m1">

var getMax = function(flightDate) {

    var now = getDate().getDate()

    var flDate = getDate(flightDate).getDate()

    if( now <flDate ){

      return '+1'

    }else{

      return ''

    }

}

var formartTime = function(flightDate,format){

  if(flightDate){

    var realDate = getDate(flightDate)

    function timeFormat(num) {

      return num <10 ? '0' + num : num

    }

    var date = {

      "Y": timeFormat(realDate.getFullYear()),

      "M": timeFormat(realDate.getMonth() + 1),

      "d": timeFormat(realDate.getDate()),

      "h": timeFormat(realDate.getHours()),

      "m": timeFormat(realDate.getMinutes()),

      "s": timeFormat(realDate.getSeconds()),

      "q": Math.floor((realDate.getMonth() + 3) / 3),

      "S": realDate.getMilliseconds(),

    }

    if (!format) {

      format = "yyyy-MM-dd hh:mm:ss"

    }

    if( format == 'hh:mm' ){

        return date.h+':'+date.m

    }else{

        return date.h+':'+date.m

    }

  }else{

    return false

  }

}

module.exports.getMax = getMax

module.exports.formartTime = formartTime

</wxs>

可在页面添加如下使用:

m1.formartTime()  m1.getMax()

1、小程序不是运行在浏览器中,所以没有DOM和BOM对象

2、小程序的JS有一些额外的成员

App方法 用于定义应用程序实例对象

Page方法 用于定义页面对象

getApp方法 用于获取全局应用程序对象

getCurrentPages方法 用来获取当前页面的调用栈(数组 最后一个就是当前页

wx对象 用来提供核心api的 官方链接: https://developers.weixin.qq.com/miniprogram/dev/api/

3、小程序JS是支持CommonJS规范的

eg:

./utils/foo.js文件

function say(msg){

    console.log("hello"+msg)

}

module.exports = {

    say:say

}

app.js文件

const foo = require("./utils/foo.js")

foo.say("hi")


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12161034.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存