简体   繁体   English

当子元素属性aria扩展为真时,向父元素添加class

[英]Add a class to parent element when child element attribute aria expanded is true

I need to give the first div a grey background when the dropdown is open.当下拉菜单打开时,我需要给第一个 div 一个灰色背景。 I've been able to apply css depending on the a tag's aria-expanding for the child elements but I'm not sure how to do that for a parent element.我已经能够应用 css 取决于 a 标签的 aria-expanding 子元素,但我不知道如何为父元素做到这一点。 I'm using Vue so I don't want to use any Jquery.我正在使用 Vue,所以我不想使用任何 Jquery。

            <li v-for="(item, index) in locations" :key="index">
              <div id="active-hov-div">
                <a
                  id="brand-nav-item-link-id"
                  class="brand-nav-item-link"
                  type="button"
                  data-bs-toggle="dropdown"
                  aria-expanded="false"
                >
                  <label class="active-hov">
                    {{item.label}}
                  </label>
                    <i class="fa fa-sort-down test-down"></i>
                    <i class="fa fa-sort-up test-up"></i>
                </a>
                <div class="dropdown-menu dropdown-menu-end locations-ul">
                  <li class="locations-li">
                    <FindACommunity />
                  </li>
                  <ul class="locations-dropdown-ul">
                  <li v-for="(item3, index3) in item.locationsTitles" :key="index3" class="nav-item" style="padding-bottom: 8px">
                    <a class="brand-nav-dropdown-link" :href="item3.url">{{item3.title}}</a>
                  </li>
                  </ul>
                </div>
              </div>
            </li>

May be you can use the index in you v-for and define the gray-bg at your styles, then use the index === 0 to deside if is needed to show the gray background,I hope it can work for you可能您可以使用v-for中的index并在 styles 中定义gray-bg ,然后使用index === 0来决定是否需要显示灰色背景,我希望它对你有用

<template>
<!-- ... -->
    <li v-for="(item, index) in locations" :key="index">
        <div id="active-hov-div" :class="{'gray-bg':index === 0}">
            <a
                id="brand-nav-item-link-id"
                class="brand-nav-item-link"
                type="button"
                data-bs-toggle="dropdown"
                aria-expanded="false"
            >
                <label class="active-hov">
                    {{item.label}}
                </label>
                <i class="fa fa-sort-down test-down"></i>
                <i class="fa fa-sort-up test-up"></i>
            </a>
            <div class="dropdown-menu dropdown-menu-end locations-ul">
    <li class="locations-li">
        <FindACommunity />
    </li>
    <ul class="locations-dropdown-ul">
        <li v-for="(item3, index3) in item.locationsTitles" :key="index3" class="nav-item" style="padding-bottom: 8px">
            <a class="brand-nav-dropdown-link" :href="item3.url">{{item3.title}}</a>
        </li>
    </ul>
    </div>
    </div>
    </li>
<!-- ... -->
</template>

<style>
/*...*/
 .gray-bg {
     background-color: gray;
 }
/*...*/
</style>


暂无
暂无

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

相关问题 如果child aria-expanded = true,则将类添加到父元素 - If child aria-expanded=true add class to parent element 当父级包含class =“ active”时,将子元素设置为aria-expanded =“ true” - Setting child element to aria-expanded=“true” when parent contains class=“active” 如何将属性aria-expanded = true / false附加到元素 - How to ad the attribute aria-expanded=true/false to an element aria-expanded =“ true”不会将类添加到父项 - aria-expanded=“true” doesn't add class to parent 当aria为true或false时,如何将addClass和removeClass添加到子元素? - How to addClass and removeClass to child element when aria true or false? javascript:仅在父元素处于活动状态且子元素具有特定类的情况下,才如何向子元素添加属性? - javascript: how to add an attribute to a child element ONLY if a parent element is active AND child element has a certain class? 即使元素展开,aria-expanded 也会显示为 false - The aria-expanded shows false even when the element is expanded 获取子代ID并添加为父代作为ARIA属性 - Get child ID and add to parent as ARIA attribute 当您在反应中单击父元素时是否可以 select 子元素,并在事件发生后将 class 添加到父元素 - is it possible to select child element when you click on parent element in react, and the add a class to the parent element after an event 如果条件为真,则将类添加到父元素 - add class to parent element if conditions are true
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM