<canvas
type="2d"
id="canvas"
style="width: 300px; height: 300px;"
></canvas>
注意:中的 id,不能写成canvas-id, 否则获取到的节点为空
js中代码
setTimeout(function(res){
wx.createSelectorQuery()
.select('#canvas')
.fields({
node: true,
size: true,
})
.exec(function (res) {
console.log(res,'RES')
})
},500)

注意:最好设置一个定时器,避免加载时获取值为 null
本文介绍了在微信小程序中如何使用SelectorQuery来获取Canvas节点,特别提醒注意节点ID不能以canvas-开头,否则可能无法正确获取。同时,建议通过设置定时器确保在正确的时间获取节点,以避免值为null的问题。

3920

被折叠的 条评论
为什么被折叠?



