[Vuejs]-VueJS DOM is not accessible from JavaScript

1👍

You should do your DOM custom operations in mounted or updated lifecycle hooks:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.common.dev.js"></script>
<body>
    <div id="app">
        <div v-for="site in topSites" class="col text-center topSites resBoo">{{site}}</div>
    </div>
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            topSites: ["site 1", "site 2"],
        },
        mounted: function () {
            const elems = document.body.querySelectorAll('.topSites');
            elems.forEach(i => {i.style.background = 'blue'; console.log('aa',i)});
        }
    })    
</script>
</html>

Leave a comment