jQuery 如何获得最高的 div 父类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17653652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get top most div parent class
提问by dzumla011
I need to get the main culpritClass
div class that holds everything after something is typed and enter is pressed and put it into a variable as a string.
我需要获取在culpritClass
输入某些内容并按下 Enter 后保存所有内容的主div 类,并将其作为字符串放入变量中。
I can't seem to get the class.
我似乎无法上课。
HTML:
HTML:
<div class="culpritClass">
<div class="inner" style="display: block;">
<div class="inner_chat"></div>
<form>
<textarea class="chat_text"></textarea>
</form>
</div><a class="culpritClass" href="#">culprit</a>
</div>
I tried the following JS but I get undefined
.
我尝试了以下 JS,但我得到undefined
.
$('.chat_text').keypress(function(e) {
if (e.keyCode == '13') {
var ru = $(this).prev().parent().attr('class');
alert(ru);
}
});
The var ru
is the line that matters.
这var ru
条线很重要。
回答by Bram Vanroy
var ru = $(this).parent().parent().parent().attr('class');
should do the trick. Even though this doesn't look like it is the neatest solution, it is the most straightforward one imo. Unless there's a function to pass an argument to a function which travels a number up, e.g. ancestor(3)
which would travel three levels up in the DOM and return that element.
应该做的伎俩。尽管这看起来不是最简洁的解决方案,但它是最直接的一种 imo。除非有一个函数可以将参数传递给一个向上传递数字的函数,例如ancestor(3)
,它将在 DOM 中向上传递三个级别并返回该元素。
But if you are just trying to find the class itself, try closest.
但是,如果您只是想找到类本身,请尝试最接近。
var ru = $(this).closest('.culpritClass');
回答by Dhaval Marthak
Try this: use .closest()
试试这个:使用.closest()
$('.chat_text').keypress(function(e) {
if (e.keyCode == '13') {
var ru = $(this).closest('.culpritClass').attr('class');
alert(ru);
}
});
You can either do like this:
你可以这样做:
var ru = $(this).parents().find('div').eq(0).attr('class');
or like this:
或者像这样:
var ru = $(this).parents().find('div:first').attr('class');
Though there are so many ways to do this, you can select the way you like :)
虽然有很多方法可以做到这一点,但您可以选择自己喜欢的方式:)
回答by CPHPython
Since you are reusing the .culpritClass
, for the sake of precision and to avoid multiple parent()
calls, use the parents()
method with the class of that element as its selector:
由于您正在重用.culpritClass
,为了精确起见并避免多次parent()
调用,请使用该parents()
元素的类作为其选择器的方法:
$('.chat_text').keypress(function(e) {
if (e.keyCode == '13') {
alert($(this).parents('.culpritClass').attr('class'));
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>Place the cursor below and press ENTER</h3>
<div class="culpritClass">
<div class="inner" style="display: block;">
<div class="inner_chat"></div>
<form>
<textarea class="chat_text"></textarea>
</form>
</div><a class="culpritClass" href="#">culprit</a>
</div>
回答by Rohan Kumar
Try this,
尝试这个,
$(this).closest('selector').find('div:first').attr('class');
If you want the parents
.culpritClass
first element then try this,
如果你想要第parents
.culpritClass
一个元素,那么试试这个,
$(this).closest('.culpritClass').find('div:first').attr('class');
回答by Bindiya Patoliya
You can also use
你也可以使用
var ru = $(this).parent('form').parent('div').parent('div').attr('class');
instead of
代替
var ru = $(this).prev().parent().attr('class');
回答by Balint Bako
I would go with $(this).parents('.culpritClass')
or if you don't "know" the class, but you know that it has 3 parents exactly, then $(this).parent().parent().parent()
我会去,$(this).parents('.culpritClass')
或者如果你不“知道”这个班级,但你知道它有 3 个父母,那么$(this).parent().parent().parent()
The problem in your JS is that the textarea has no prev()
, you should change that too parent()
and it will work.
你的 JS 中的问题是 textarea 没有prev()
,你也应该改变它parent()
,它会起作用。
回答by Yogesh
$(document).ready( function() {
$('.chat_text').keypress(function(e) {
if (e.keyCode == '13') {
var ru = $(this).closest("div").parent();
alert($(ru).html());
}
});
});
});