目录

通用方法

focus() (1040+,非厂商联盟规范)

input和textarea组件支持通用方法focus()设置焦点,在组件对应的view上面设置焦点后,会反向触发onfocus事件。

名称

参数

描述

focus

object(非必选参数)

{focus:true|false},focus不传默认为true,表示设置焦点。

针对组件设置焦点。

具体使用方法:

this.$element('xxx').focus()

getViewId()(1060+)

所有组件都支持通用方法getViewId()获取组件的nodeId。

名称

参数

描述

getViewId

-

获取组件的nodeId,返回为Number类型。

具体使用方法:

this.$element('xxx').getViewId()

addEventListener(String type, JSCallback callback) (1070+)

所有组件都支持通过方法addEventListener(String type, JSCallback callback)添加CSS动画监听事件。

名称

参数

描述

addEventListener

type: typevalue

callback: callbackvalue

添加CSS动画事件的监听器。

  • type:动画事件类型。可选值为:animationstart、animationiteration和animationend。
  • callback:类型为JSCallback,回调事件。
说明:

如果要获取callback结果,必须先调用addEventListener,然后再动态修改class,触发CSS动画样式效果。

具体使用方法:

var element = $element("id")
element.addEventListener("animationstart", (event) => {
    prompt.showToast({
        message: 'type: ' + event.type + ', animationName: ' + event.animationName + ', elapsedTime: ' + event.elapsedTime
    })
})
element.addEventListener("animationend", (event) => {
    prompt.showToast({
        message: 'type: ' + event.type + ', animationName: ' + event.animationName + ', elapsedTime: ' + event.elapsedTime
    })
})
element.addEventListener("animationiteration", (event) => {
    prompt.showToast({
        message: 'type: ' + event.type + ', animationName: ' + event.animationName + ', elapsedTime: ' + event.elapsedTime
    })
})

toTempFilePath(Object object) (1070+)

将组件当前绘制区域的内容导出生成图片或pdf文档,并保存至临时文件夹。

video、camera、span、option组件不支持该方法。

map组件只支持单独导出地图底图,当导出包含map组件的父组件时,无法导出map组件内容区域。

Object参数说明

参数

类型

默认值

是否必填

描述

fileType

string

png

否

导出文件的类型。

  • jpg:jpg图片
  • png:png图片
  • pdf(1070+):pdf文档(非厂商联盟规范)

quality

number

1.0

否

图片的质量,目前仅对 jpg 有效。取值范围为 (0,1],不在范围内的值当作 1.0 处理。

success

function

-

否

接口调用成功的回调函数。

fail

function

-

否

接口调用失败的回调函数。

complete

function

-

否

接口调用结束的回调函数(调用成功、失败都会执行)。

success返回值

参数

类型

描述

tempFilePath

string

导出内容保存的临时文件路径。

fail返回错误码

错误码

说明

200

一般性错误。

203

当前组件不支持导出图片。

300

文件保存失败。

具体使用方法:

this.$element('xxx').toTempFilePath({
    fileType: 'jpg', quality: 1.0, success: function (ret) {
        console.debug('toTempFilePath success:tempFilePath=' + ret.tempFilePath)
    }, fail: function (msg, code) {
        console.log('toTempFilePath failed:code=' + code + '; msg=' + msg);
    }, complete: function (ret) {
        console.log('toTempFilePath complete');
    }
}

getBoundingClientRect(Object object) (1070+)

获取当前组件的布局位置。

  • popup、option、span、picker 等组件不支持调用此方法。
  • 由于在onInit方法中组件还没有创建,所以在onInit方法中调用此方法会出现js错误。
  • 由于在onReady方法中组件还没有渲染完成,所以在onReady方法中调用此方法获取结果都为0。请勿在onReady之前和onReady中调用此方法

Object参数说明

参数

类型

是否必填

说明

success

function

否

接口调用成功的回调函数。

fail

function

否

接口调用失败的回调函数。当获取高度和宽度都为0时调用。

complete

function

否

执行结束后的回调。

success返回值

参数

类型

说明

left

number

节点的左边界坐标。

right

number

节点的右边界坐标。

top

number

节点的上边界坐标。

bottom

number

节点的下边界坐标。

width

number

节点的宽度。

height

number

节点的高度。

示例代码

<template>
    <div class="container">
        <div class="item-container">
            <input id="btnid" type="button" style="width:600px" value="queryBounds" onclick="queryBounds"/>
            <text>{{rect}}</text>
        </div>
    </div>
</template>
<style>
    @import '../../../common/css/common.css';
    .item-container {
        margin-top: 40px;
        margin-bottom: 40px;
        margin-left: 40px;
        margin-right: 40px;
        flex-direction: column;
    }
</style>
<script>
    export default {
        data: {
            rect:"click button to query bounds"
        },
        onInit: function () {
        },
        queryBounds: function () {
            var that = this;
            this.$element('btnid').getBoundingClientRect({
                success: function (data) {
                    that.rect = data;
                    console.log('getBoundingClientRect succeeded, result data =' + JSON.stringify(data));
                },
                fail() {
                    console.log('getBoundingClientRect failed');
                },
                complete: function () {
                    console.log('getBoundingClientRect completed');
                }
            });
        }
    }
</script>

输出日志信息:

12-26 22:28:34.008 I/jsLog   ( 8474): getBoundingClientRect succeeded, result data ={"top":40.27778,"left":40.27778,"bottom":115.27778,"width":600,"right":640.2778,"height":75}
12-26 22:28:34.008 I/jsLog   ( 8474): getBoundingClientRect completed

相关链接

[h2]FAQ

快应用中有方法获取某个组件的布局位置吗?