如何获得最高的div家长班 [英] How to get top most div parent class

查看:149
本文介绍了如何获得最高的div家长班的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取主culpritClass div类,该主div类在键入某些内容并按下enter键后将所有内容保存为字符串.

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.

我好像没办法上课.

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>

我尝试了以下JS,但得到了undefined.

I tried the following JS but I get undefined.

$('.chat_text').keypress(function(e) {
    if (e.keyCode == '13') {

        var ru = $(this).prev().parent().attr('class');

        alert(ru);
    }
});

var ru是重要的一行.

推荐答案

var ru = $(this).parent().parent().parent().attr('class');

应该可以解决问题.即使这看起来不是最整洁的解决方案,它也是最简单的一个imo.除非有一个函数将参数传递给向上移动数字的函数,例如ancestor(3)将在DOM中向上移动三层并返回该元素.

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.

但是,如果您只是试图查找类本身,请尝试最接近.

But if you are just trying to find the class itself, try closest.

var ru = $(this).closest('.culpritClass');

这篇关于如何获得最高的div家长班的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆