一、HTML中获取DOM元素
1.通过querySelector是静态获取的,而是用getElementsByClassName是动态获取,也就是说每一次使用都会去对dom节点调用一次
<div id="app">
<ul class="ul">
<li>你好</li>
<li>你好</li>
<li>你好</li>
</ul>
</div>
<script>
let app = document.getElementById('app')
let ul = document.getElementsByClassName('ul')
let ul1 = document.querySelector('.ul')
console.log(app);
console.log(ul[0]);
let lis = document.querySelectorAll('.ul li')
console.log(lis.length);
for(let i=0; i< lis.length;i++){
console.log(ul1);
ul1.appendChild(document.createElement('li'))
}
console.log(ul[0].innerHTML);
</script>
一、Vue中获取DOM元素
<div ref="app"><div>
<script>
// 获取DOM元素
this.$refs.app.$el.style.top = 0
this.$refs.app.$el.style.position = 'fixed'
this.$refs.app.$el.getBoundingClientRect()
</script>
二. uni-app中获取DOM元素
<view class="app"></view>
<script>
const query = uni.createSelectorQuery().in(this)
uni.createSelectorQuery().select('.app').fields({
node: true,
size: true
})
.exec(function(res) {
console.log(res);
})
</script>