撸码成魔

微信小程序中利用DOM获取页面元素的信息

电脑版发表于:2019/5/8 11:05

在js中,我们常常用到类似    $("#id")  这样的代码获得一个页面元素对象  但是在微信小程序中是不支持的,那么我们难免有时候会需要这样一个方法去拿到想要的对象,如下:


//首先给你的元素对象一个id:
<view class="usermotto" style="height:213px;" id='mjltest'/>

//然后在js里,用一个SelectorQuery来选择对应id的节点(注意id前面要加一个#号),就可以获取对应节点的属性,包括高度。

//创建节点选择器
var query = wx.createSelectorQuery();
query.select('#mjltest').boundingClientRect()
query.exec(function (res) {
//res就是 所有标签为mjltest的元素的信息 的数组
console.log(res);  
//取高度
console.log(res[0].height);
})



关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢