<view ref="st"></view>



	this.$refs.st.$el.style.background="#fff"
				console.log(this.$refs.st.$el.style.background)
//微信小程序里面 获取dom ,ref微信小程序用不了

export const SelectorQuery = (id, that) => {
	return new Promise((resolve, reject) => {
	let query = uni.createSelectorQuery().in(that).select(id).boundingClientRect(function(data) {
			resolve(JSON.stringify(data)) 
		}).exec();
	})
}

//按需引入
import {
		SelectorQuery
	} from "@/api/index.js"
//使用
async onLoad() {
			console.log( await SelectorQuery(".content",this))
		},
//或者在页面直接用
	onLoad() {
			this.$nextTick( () => {
			let query = uni.createSelectorQuery().select('#first').boundingClientRect(function(data) {
				console.log(1,data)
			}).exec();
			})

			// this.$nextTick( () => {
			// 	//必须先创建SelectorQuery对象的实例,该实例用来查询DOM节点的信息
			// 	const query = uni.createSelectorQuery()
			// 	//匹配选择器的第一个节点,selectAll匹配选择器的所有节点
			// 	query.select('#first').boundingClientRect( rect => {
			// 		//rect是包含元素大小、位置等数据的对象
			// 		console.log(2,rect)
			// 	}).exec(() => {
			// 		//节点信息获取成功后执行的回调函数
			// 	})
			// })
		}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐