<template>
<div class="wrapper" id="wrapper">
<h1>1212121</h1>
<h2>1212121212</h2>
<div class="">
<div class="">
<div class="">121212</div>
</div>
</div>
<span>121212</span>
</div>
</template>
<script>
export default {
mounted() {
const arr = getAllNodes(document.getElementById("wrapper"));
console.log(arr);
function getAllNodes(dom) {
const doms = [];
const { childNodes } = dom;
[...childNodes].map((item) => {
const { nodeType } = item;
if (nodeType === 1) {
doms.push(item, ...getAllNodes(item));
}
});
return doms;
}
},
};
</script>
<script>
export default {
mounted() {
const arr = getAllNodes(document.getElementById("wrapper"));
console.log(arr);
function getAllNodes(dom) {
const doms = [];
const { children } = dom;
[...children].map((item) => {
doms.push(item, ...getAllNodes(item));
});
return doms;
}
},
};
</script>
网友评论