我有一个如下所示的组件布局:

<div class="list-item" v-for="item in items" :key="item.id">
  <div class="sub-item"></div>
</div>

我遇到的问题是,我如何按需获取该子项元素的句柄?我基本上需要做这样的事情:

  1. 从条目ID=X的列表中获取条目.
  2. 抓取所选项目下的".subItem"HTMLElement以动态调整元素"yle".

先谢谢你.

推荐答案

要更改for循环中具有特定id的元素,可以在v-for中使用condition,如下所示:

<div id="container" v-for="item in items" :key="item.id">
  <div :class="item.id == ID ? 'sub-item change-width-class' : 'sub-item'"></div>
</div>

您可以监听计时器,并使用watcher:

<template>
    <div id="container" v-for="item in items" :key="item.id">
        <div :id="`sub-item-${currentElementId}`></div>
    </div>
</template>

<script setup>
    import { ref, watch } from 'vue';

    const timer = ref(0);
    const currentElementId = ref(1);

    // every time timer updates the function below will run
    watch(timer, () => {
        const subItem = document.querySelector(`.sub-item-${currentElementId}`);
        subItem.style.width = (100 * timer / 250); 
    });

</script>

Vue.js相关问答推荐

vue 不重新渲染数据更改

在 Vue 中删除自定义组件代码重复的方法?

我如何使用 vuejs 在我的 url 中编码一个令牌

拖入 vue2-google-map 后如何获取标记位置?

Vuejs 和 Laravel 发布请求 CORS

Nuxt.js 中的开发工具样式编辑问题

apache上的Vue-router,子目录下的SPA,只能通过重定向访问页面

在 SASS 中使用 Vuetify 类

如何在 vue 3 脚本设置中使用

VueJS 组件中的图像未加载

使用 vue-property-decorator 时注册本地组件

事件 click点击的无效处理程序:未定义

怎样1秒后自动隐藏VueJS中的元素

如何避免在 Vue 中一直写 this.$store.state.donkey 的需要?

如何将事件绑定到 Vuetify 中的树视图 node ?

Vue在按钮单击时添加一个组件

嵌套组件

如何使用 Vue Test utils 测试输入是否聚焦?

使密码字符隐藏在 Vuetify 字段中

在子元素 vueJS.2 之间切换active类