您的位置:首页 > Web前端 > JQuery

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

2015-01-26 21:51 561 查看
有一个Div,我想获取其ID或者Class值。

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

假如说:有一段Html代码:

[html] view
plaincopy

<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值为:

[javascript] view
plaincopy

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

其值为:22;
CLASS值为:

[javascript] view
plaincopy

$(".submit").parent().parent().parent().parent().attr('class')  

其值为:comment;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐