如何用Jquery获取某一个Div的Class或者ID

来源:互联网 发布:怎么查淘宝宝贝降权 编辑:程序博客网 时间:2024/06/09 23:49

有一个Div,我想获取其ID或者Class值。

那么我可以先定位到这个DIV,然后采用attr方法来获取其值:

假如说:有一段Html代码:

<div class="comment" id="22"><div class="comment_detail" style="display: block; "><div class="comment_my"><textarea class="comment_text" style="height: 16px; "></textarea></div><div class="comment_text_bottom" style="display: none; "><div class="comment_emotion"><a href="javascript:void(0)" class="comment_emotion_button">表情</a><div class="comment_emotion_detail" style="display: none; "></div></div><div class="comment_publish"><input class="submit" type="submit"  value="评论"></div></div></div><div class="comment_bar"><a href="javascript:void(0)" class="comment_click"></a></div></div>

如果我想从submit处定位,获取到comment处DIv的ID,那么我就可以这样办:

ID值为:

$(".submit").parent().parent().parent().parent().attr('id');

其值为:22;

CLASS值为:

$(".submit").parent().parent().parent().parent().attr('class')
其值为:comment;


原创粉丝点击