如何使用原型从包装器 div 获取所有外部(父)divs id 值?

how to get all outer(parent) divs id values from wrapper div using prototype?

我只想从 HTML 结构下面获取第一个外部所有 divs id 值。

我想输出 p1,test,contents 这样的输出 divs ids value("p1,test,contents") in "padder" div not all divs id value 。我试过下面的代码 但它 returns 所有 div ids 值,但我想 only outer divs id 它的值像 p1,test,contents(这是我想要的外部 divs id 和它的值)在下面的 html 结构中。

$('padder').select('div').each(function(el){
  alert($(el).id);
}); 

<div id="padder">
  <div id="p1">
    <div id="s1">
    </div><!-- Ends s1 div-->
  </div><!-- Ends p1 div-->
  <div id="test">
    <div class="subdiv">
    </div><!-- Ends subdiv div-->
  </div><!-- Ends test div-->
  <div id="contents">
    <div>
    </div><!-- Ends div-->
  </div><!-- Ends contents div-->  
</div><!-- Ends padder div-->   

您可以使用 select 或 #padder > div 来 select 所有 div 元素,它们是 #padder 的直接子元素。

然后遍历元素,使用 el.id (example)

访问 id
$$('#padder > div').each(function(el) {
    alert(el.id);
});

您还可以将 id 映射到数组 (example) -(没有 prototype.js)

var ids = Array.prototype.map.call(document.querySelectorAll('#padder > div'), function (el, i) {
    return el.id;
});

输出:

["p1", "test", "contents"]