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

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

可以先定位到这个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;

1、新建一个html文件,命名为testhtml。

2、在testhtml文件内,使用div标签创建一行文字,文字内容为“这是测试的文字内容”。

3、在testhtml文件内,设置div的id属性为abc,用于下面通过该id获得div对象。

4、在testhtml文件内,使用button标签创建一个按钮,按钮名称为“获取div里面的值”。

5、在testhtml文件中,给button按钮绑定onclick点击事件,当按钮被点击时,执行getdivc()函数。

6、在js标签中,创建getdivc()函数,在函数内,通过id(abc)获得div对象,使用text()方法获得div内的值,并使用alert()方法将值输出。

7、在浏览器中打开testhtml文件,点击按钮,查看结果。

以上就是关于如何用Jquery获取某一个Div的Class或者ID全部的内容,包括:如何用Jquery获取某一个Div的Class或者ID、jquery怎样获得所有div的id值,有多个div。用$("div").attr(id)只能获得一个div的id值、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址:https://54852.com/web/9314711.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存