如何循环div的子div并获取子div的id? [英] how to loop over the child divs of a div and get the ids of the child divs?

查看:548
本文介绍了如何循环div的子div并获取子div的id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有id测试的div

I have a div with id test

并且通过foreach循环我在测试div中创建了一些内部div。所以就是这样。

and through the foreach loop I am creating some inner divs inside the test div. So it becomes like this.

<div id="test">
<div id="test-1"></div>
<div id="test-2"></div>
<div id="test-3"></div>
<div id="test-4"></div>
</div>

我在javascript函数中获得父divtest。现在我想循环遍历测试div的内部div(子div)并逐个获取每个div的id并通过javascript设置它们。

I am getting the parent div id "test" in the javascript function. Now I want to loop through the inner divs(child divs) of the test div and get the id of the each div one by one and style them through javascript.

任何关于这个的想法?

推荐答案

你可以使用jQuery .each()函数循环遍历内部div。下面的示例执行此操作,并为每个内部div获取id属性。

You can loop through inner divs using jQuery .each() function. The following example does this, and for each inner div, it gets the id attribute.

$('#test').find('div').each(function(){
    var innerDivId = $(this).attr('id');
});

这篇关于如何循环div的子div并获取子div的id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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