简体   繁体   English

滚动到同一文档中两个不同列表中具有相同 id 值的元素

[英]Scroll to elements from two different lists in the same document that have the same id value

In the following case there are two lists, one being nested within the other.在下面的例子中,有两个列表,一个嵌套在另一个列表中。

<q-list>
    <q-item
        v-for="item in data"
        :key="item.id_data"
        :id="item.id_data"
        >
        <q-item-section>
            <div>
                <span {{ item.name }} </span>

                <q-item
                    v-for="stuff in item"
                    :key="stuff.id_stuff"
                    :id="stuff.id_stuff">
                    <q-item-section>
                        <span"> {{ stuff.name }} </span>
                    </q-item-section>
                </q-item>

            </div>
        </q-item-section>
    </q-item>
</q-list>

Two click events can be triggered that generated a scroll to a element of the lists:可以触发两个单击事件,从而产生对列表元素的滚动:

1- In the first case, the scroll must be done for the element to which the id corresponds to item.id_data 1-在第一种情况下,必须为id对应的元素完成滚动item.id_data

2- In the second case, the scroll must be done for the element to which the id corresponds to stuff.id_stuff 2- 在第二种情况下,必须为id对应于stuff.id_stuff的元素完成滚动

This means that the data id value can match the stuff id value (ie, item.id_data = 4 , stuff.id_stuff = 4 ).这意味着数据 id 值可以匹配 stuff id 值(即 item.id_data = 4 , stuff.id_stuff = 4 )。

This causes problems when using document.getElementById(id).这在使用 document.getElementById(id) 时会导致问题。

Example of the scrollTo function:滚动到 function 的示例:

scrollTo (id) {
    const ele = document.getElementById(id)
    const target = getScrollTarget(ele)
    const offset = ele.offsetTop - ele.scrollHeight    
    const duration = 500
    setScrollPosition(target, offset, duration)
},

how to solve this?如何解决这个问题?

You could bind a prefix to each ID to differentiate them:您可以为每个 ID 绑定前缀以区分它们:

<q-list>
    <q-item
        v-for="item in data"
        :key="item.id_data"
               👇
        :id="'data_' + item.id_data"
        >
        <q-item-section>
            <div>
                <span {{ item.name }} </span>

                <q-item
                    v-for="stuff in item"
                    :key="stuff.id_stuff"
                            👇
                    :id="'stuff_' + stuff.id_stuff">
                    <q-item-section>
                        <span"> {{ stuff.name }} </span>
                    </q-item-section>
                </q-item>

            </div>
        </q-item-section>
    </q-item>
</q-list>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM