search

Home  >  Q&A  >  body text

javascript - Vue2.0 How to get the index in the event without v-for loop?

RT

In addition to using native, are there any methods and techniques that come with Vue?
Thank you~

phpcn_u1582phpcn_u15822742 days ago634

reply all(4)I'll reply

  • 怪我咯

    怪我咯2017-05-19 10:31:29

    var el = document.getElementById('container-id');
    
    el.addEventListener('click', function(e) {    
        var p = e.target.parentElement;
        var index = Array.prototype.indexOf.call(p.children, e.target);
    }
    

    Just giving you an idea


    <p class="container" ref="containBox">
        <span @click="getIndex">test</span>   
        <span>test</span>
        <span>test</span>
        <span>test</span>
        <span>test</span>
    </p>
    export default {
        methods: {
            getIndex (e) {
                const parent = this.$refs.containBox.xxx 
                // 上面我忘记xxx是什么了,反正可以获取父元素,console.log(this.$refs)看看
                const index = Array.prototype.indexOf.call(parent.children, e.target)
                // ...
            }
        }
    }

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-19 10:31:29

    Where does the index come from without a loop?? Where does the index refer to?

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-19 10:31:29

    Use native document.queraySelectAlll to give li the same class name to get the li array. Now there is an index

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-19 10:31:29

    I don’t quite understand what you mean. . . .
    How to load list data without using v-for? To find the node sequence number according to the dom, you must also have the dom node. . .

    reply
    0
  • Cancelreply