可以通过在点击事件中添加样式类来改变选中节点的字体颜色。具体的实现步骤如下:

  1. 在点击事件处理函数 handleClick 中,获取点击的节点,并将其字体颜色改成红色。
  2. 在 handleClick 函数中添加以下代码:
function handleClick(ev) {
    console.log('ev', ev);
    console.log('ev.categoryId 点击的这个类目id是----', ev.categoryId);
    loading.value = true;
    queryParams2.categoryId = ev.categoryId;
    getSpecsPage(queryParams2)
        .then((data: any) => {
            SpecsList.value = data.rows;
            total.value = data.total; //页
        })
        .finally(() => {
            loading.value = false;
        });

    // 获取所有节点
    const nodes = document.getElementsByClassName("el-tree-node");

    // 移除所有节点的选中样式
    for (let i = 0; i < nodes.length; i++) {
        nodes[i].classList.remove("selected-node");
    }

    // 将点击的节点添加选中样式
    ev.target.parentNode.classList.add("selected-node");
}
  1. 在 CSS 中定义 .selected-node 样式类,将字体颜色改为红色:
.selected-node {
    color: red;
}
  1. 在模板中的 el-tree 组件上添加 class-name 属性,将其值设置为 "custom-tree",以便应用样式类:
<el-tree class-name="custom-tree" :data="treeData" :props="defaultProps" node-key="categoryId" default-expand-all
    @node-click="handleClick" />
  1. 在样式中添加 .custom-tree 样式类,并设置其字体颜色为黑色:
.custom-tree {
    color: black;
}

这样,当点击某个节点时,该节点的字体颜色将改变为红色。其他节点的字体颜色保持为黑色

function handleClickev consolelogev ev; consolelogevcategoryId 点击的这个类目id是---- evcategoryId; loadingvalue = true; queryParams2categoryId = evcategoryId; getSpecsPagequeryParams2

原文地址: https://www.cveoy.top/t/topic/iGyg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录