简体   繁体   English

如何遍历一组ID并匹配jQuery中另一组元素的活动元素属性?

[英]How can I loop through a set of IDs and match the active element's attribute of another set of elements in jQuery?

I'm trying to loop through a set of element's IDs and match it against another set of element's attribute (aria-labelledby) but only if it is the active panel and I cannot find a solution to it. 我试图遍历一组元素的ID,并将其与另一组元素的属性(aria-labelledby)匹配,但前提是它是活动面板且我无法找到解决方案。

 $('body a').mouseenter(function() { e = $(this); btnClass = e.attr('class'); switch (btnClass) { case 'nav-link': tabID = $(this).attr('id'); paneID = $('#v-pills-tabContent').children(); paneID.each(function(index) { console.log(index + ": " + $(this).prop('id')); }); break } }); 
 .nav-link { display: block; text-decoration: none !important; padding: 14px; border-bottom: 1px solid #eff1f2; color: #747474; } #v-pills-tab a:last-child { border-bottom: 0; } .tab-content > .active { opacity: 1; } .col-3,.col-9 { float: left; } .col-9 { width: 250px; } .col-3 { border: 1px solid #eff1f2; } #v-pills-tabContent p { margin-bottom: 18px; } .day-num,.day-name,.month-name,.year-name { display: block; text-align: center; } .day-num,.month-name,.schedule-header { font-size: 28px; } .day-name,.year-name { font-size: 14px; } .schedule-header { display: block; text-align: center; line-height: 3.2em; } .radio-link { padding: 10px 20px; position: relative; } .schedule-radio { width: 18px; height: 18px; background: #fff; border: 2px solid #eff1f2; border-radius: 50%; display: inline-block; border-radius: 25px; margin: 0 10px 0 0; position: absolute; left: -6px; top: 8px; } .tab-content .no-top-margin { margin-top: 0 !important; } .tab-content > .tab-pane { margin-top: 22px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <div class="col-3"> <div class="nav flex-column nav-pills" id="v-pills-tab" role="tablist"> <div class="nav-link" role="tab" aria-expanded="true"><span class="month-name">Oct</span><span class="year-name">2017</span></div> <a class="nav-link" id="v-pills-home-tab" data-toggle="pill" href="#v-pills-home" role="tab" aria-controls="v-pills-home" aria-expanded="true"> <span class="day-num">9</span> <span class="day-name">Thursday</span> </a> <a class="nav-link" id="v-pills-profile-tab" data-toggle="pill" href="#v-pills-profile" role="tab" aria-controls="v-pills-profile" aria-expanded="true"> <span class="day-num">10</span> <span class="day-name">Friday</span> </a> <a class="nav-link" id="v-pills-messages-tab" data-toggle="pill" href="#v-pills-messages" role="tab" aria-controls="v-pills-messages" aria-expanded="true"> <span class="day-num">11</span> <span class="day-name">Saturday</span> </a> <a class="nav-link" id="v-pills-settings-tab" data-toggle="pill" href="#v-pills-settings" role="tab" aria-controls="v-pills-settings" aria-expanded="true"> <span class="day-num">12</span> <span class="day-name">Sunday</span> </a> </div> </div> <div class="col-9"> <div class="tab-pane no-top-margin" id="v-pills-currentdate" role="tabpanel" aria-labelledby="v-pills-currentdate-tab"> <p style="margin-bottom:0;" class="no-top-margin"><span class="schedule-header">Schedule</span></p> </div> <div class="tab-content" id="v-pills-tabContent"> <div class="tab-pane fade active" id="v-pills-home" role="tabpanel" aria-labelledby="v-pills-home-tab"> <p><a href="" class="radio-link" id=""><span class="schedule-radio"></span><span><time datetime="2017-10-13T00:25:00Z">4:05pm CST</time></span></a></p> <p><a href="" class="radio-link" id=""><span class="schedule-radio"></span><span><time datetime="2017-10-13T00:25:00Z">6:25pm CST</time></span></a></p> <p><a href="" class="radio-link" id=""><span class="schedule-radio"></span><span><time datetime="2017-10-13T00:25:00Z">7:35pm CST</time></span></a></p> <p><a href="" class="radio-link" id=""><span class="schedule-radio"></span><span><time datetime="2017-10-13T00:25:00Z">8:25pm CST</time></span></a></p> </div> <div class="tab-pane fade" id="v-pills-profile" role="tabpanel" aria-labelledby="v-pills-profile-tab"> <p><a href="" class="radio-link" id=""><span class="schedule-radio"></span><span><time datetime="2017-10-13T00:25:00Z">5:35pm CST</time></span></a></p> <p><a href="" class="radio-link" id=""><span class="schedule-radio"></span><span><time datetime="2017-10-13T00:25:00Z">7:25pm CST</time></span></a></p> <p><a href="" class="radio-link" id=""><span class="schedule-radio"></span><span><time datetime="2017-10-13T00:25:00Z">8:15pm CST</time></span></a></p> <p><a href="" class="radio-link" id=""><span class="schedule-radio"></span><span><time datetime="2017-10-13T00:25:00Z">9:45pm CST</time></span></a></p> </div> <div class="tab-pane fade" id="v-pills-messages" role="tabpanel" aria-labelledby="v-pills-messages-tab"> <p><a href="" class="radio-link" id=""><span class="schedule-radio"></span><span><time datetime="2017-10-13T00:25:00Z">3:10pm CST</time></span></a></p> <p><a href="" class="radio-link" id=""><span class="schedule-radio"></span><span><time datetime="2017-10-13T00:25:00Z">4:35pm CST</time></span></a></p> <p><a href="" class="radio-link" id=""><span class="schedule-radio"></span><span><time datetime="2017-10-13T00:25:00Z">4:55pm CST</time></span></a></p> <p><a href="" class="radio-link" id=""><span class="schedule-radio"></span><span><time datetime="2017-10-13T00:25:00Z">5:15pm CST</time></span></a></p> </div> <div class="tab-pane fade" id="v-pills-settings" role="tabpanel" aria-labelledby="v-pills-settings-tab"> <p><a href="" class="radio-link" id=""><span class="schedule-radio"></span><span><time datetime="2017-10-13T00:25:00Z">6:15pm CST</time></span></a></p> <p><a href="" class="radio-link" id=""><span class="schedule-radio"></span><span><time datetime="2017-10-13T00:25:00Z">7:20pm CST</time></span></a></p> <p><a href="" class="radio-link" id=""><span class="schedule-radio"></span><span><time datetime="2017-10-13T00:25:00Z">7:30pm CST</time></span></a></p> <p><a href="" class="radio-link" id=""><span class="schedule-radio"></span><span><time datetime="2017-10-13T00:25:00Z">8:25pm CST</time></span></a></p> </div> </div> </div> 

I need to find the active panel and get its attribute (aria-labelledby). 我需要找到活动面板并获取其属性(aria-labelledby)。 The attribute (aria-labelledby) will be used to match an ID from another set of elements. 该属性(aria-labelledby)将用于匹配另一组元素中的ID。 This should be triggered by the jQuery mouseenter event. 这应该由jQuery mouseenter事件触发。

For better understanding, I have a live example on JSFiddle 为了更好地理解,我在JSFiddle上有一个实时示例

If you want to find the child of #v-pills-tabContent that has the attribute aria-labelledby equal to the id of the clicked a element, this is what you should do: 如果要查找属性aria-labelledby等于单击元素的id#v-pills-tabContent a子级,则应这样做:

tabID = $(this).attr('id');
paneID = $('#v-pills-tabContent')
             .children('[aria-labelledby="' + tabID + '"].active')

The selector '[aria-labelledby="' + tabID + '"].active' will match the active elements that have aria-labelledby equal to tabID . 选择器'[aria-labelledby="' + tabID + '"].active'将匹配具有aria-labelledby tabID等于tabID的活动元素。

Every active tab should have .active class. 每个活动选项卡都应具有.active类。
If so, inside the switch statement you can use $('.tab-pane.active').attr('aria-labelledby') to get the aria-labelledby value. 如果是这样,则可以在switch语句中使用$('.tab-pane.active').attr('aria-labelledby')获得aria-labelledby值。

暂无
暂无

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

相关问题 jQuery:如何遍历一组仅查找与另一个数组中的值匹配的元素? - jQuery: How can I loop over a set of elements finding only those matching the values within another array? 如何在MVC中设置元素的属性 - How can i set the attribute of an element in MVC 我可以使用jQuery根据其他属性的值设置属性吗? - Can I set an attribute based on the value of another attribute using jQuery? 我是如何在Polymer 1.0元素中循环访问一组DOM元素的? - How am I meant to loop through a set of DOM elements within a Polymer 1.0 Element? 循环遍历foreach循环中的元素并从值中提取首字母缩写词,然后将另一个元素的值设置为派生的首字母缩略词 - Loop through an element in the foreach loop and extract the acronym from the value and then set another element's value to the derived acronym 根据子元素href属性将活动类设置为元素 - set active class to element based on child elements href attribute "jQuery:如何遍历具有数据属性的元素" - jQuery: how to loop through elements with data attribute 我可以通过JQuery将CSS类交换为特定的元素集吗? - Can I swap CSS classes for a particluar Set of element through JQuery? 如何从元素的属性设置元素的标题? - How can I set the title on an element from an attribute of the element? 如何通过jQuery中的元素索引设置数据属性值? - How do I set data attribute value by the index of element in jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM