简体   繁体   English

如何获取父元素的类?

[英]How can I get the class of a parent element?

I would like to find the class of the parent element of the child with .current .我想用.current找到子元素的父元素的class

 var currentli = $('.nav').find('a.current').parent().attr('class'); console.log(currentli);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="nav"> <ul> <li class="tab1"> <a href="#1" class="current">Welcome</a> </li> <li class="tab2"> <a href="#2">About</a> </li> <!-- and some lists more --> </ul> </div>

but it always throws me undefined但它总是让我undefined

NOTE: I don't know which a currently has .current which is why I have to use the .find() method.注:我不知道哪a目前有.current这就是为什么我必须使用.find()方法。

Your code should already work, but it might be more reliable to access the property className instead:您的代码应该已经可以工作了,但访问属性className可能更可靠:

 jQuery(function($) { console.log($('.nav a.current').parent().prop('className')); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <div class="nav"> <ul> <li class="tab1"> <a href="#1" class="current">Welcome</a> </li> <li class="tab2"> <a href="#2">About</a> </li> <!-- and some lists more --> </ul> </div>

This will work:这将起作用:

var currentli = $($('.nav').find('a.current')).parent().attr('class');
console.log(currentli);

I'm simply transforming the found collection into a jQuery object again.我只是将找到的集合再次转换为 jQuery 对象。

See fiddle: http://jsfiddle.net/RaphaelDDL/wnW4u/见小提琴: http : //jsfiddle.net/RaphaelDDL/wnW4u/

Ps.: If more than one a has class="current" , only the first one will be retrieved. Ps.:如果有多个a具有class="current" ,则只会检索第一个。

 jQuery(function($) { console.log($('.nav a.current').parent().prop('className')); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <div class="nav"> <ul> <li class="tab1"> <a href="#1" class="current">Welcome</a> </li> <li class="tab2"> <a href="#2">About</a> </li> <!-- and some lists more --> </ul> </div>

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

相关问题 如何隐藏具有给定类的所有元素的父元素,但父元素也具有特定的类? - How can I hide parent element of all elements with a given class, but the parent element also has a specific class? 如何根据嵌套元素的 class 更改父 div - How can I change parent div based on a class of nested element 如何使用JavaScript从父元素的父元素ID中获取元素的属性? - How can I get an element's attribute from the parent's parent element's ID using JavaScript? 获取 Parent 不是类的元素 - Get Element that Parent not a class 如何通过类名获取位于父元素中的元素的值 - How to get the value of an element located in the parent element by class name 如果我只知道父元素的类或ID,如何在子textarea上设置模糊侦听器? - If I only know a class or ID of a parent element, how can I set a blur listener on a child textarea? 我如何获得元素的父元素? - How do I get the parent of an element? 如何获取具有特定 class 的元素的 innerhtml 文本? - How can I get the innerhtml text of a element with a specific class? 如何获得带有类的元素的当前索引? (jQuery的) - How can I get the current index of an element with a class? (JQuery) 如果有很多同一个类,如何使用JavaScript获取特定元素? - How can I get a particular element with JavaScript if there are many with same class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM