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

如何用Jquery获取某一个Div的Class或者ID,第1张

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

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

假如说:有一段Html代码:

[html] view plain copy

<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 plain copy

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

其值为:22;

CLASS值为:

[javascript] view plain copy

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

其值为:comment;

jquery 通过class获得id方法如下:

$("类名")取对象

$("类名")attr("id") 取对象的ID

JQuery是继prototype之后又一个优秀的Javascript库。它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 60+, FF15+, Safari 20+, Opera 90+),jQuery20及后续版本将不再支持IE6/7/8浏览器。

$("#textId")attr("width")是获取Id为textId的控件的width属性

$("className")attr("width")是获取包含class名为className的控件的width属性。

$("[name=textName]")attr("width")是获tml xmlns=">

取name为textName的控件的width属性。

但要注意,jquery取到的可能是一个数组,如果确定属性为同一值,则没问题,否则应该循环取得控件,再取属性值。

如:$("#textId")[0]width是取第一个控件的width属性。

但也有写属性用attr取不到,也需要用角标的方式取得控件后在取属性。

以上就是关于如何用Jquery获取某一个Div的Class或者ID全部的内容,包括:如何用Jquery获取某一个Div的Class或者ID、jquery 通过class获得id、jquery怎么获得元素的属性id值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9389648.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存