这应该完全回答我的问题:
How to get the first child id inside the div using JQuery
但是当我尝试它时,它返回父元素本身的ID。我的HTML是:
<div id="top-right-container">
<div class="panel panel-inverse" data-sortable-id="products">
<div class="product-heading">
<h4 class="product-title">Products</h4>
</div>
<div class="product-body">
<span class="product-text">Product</span>
<div>
<div id="product-element"></div>
</div>
</div>
</div>
</div>
我正在使用的js是:
var topRightContainer = $("#top-right-container:first-child").attr("id");
var bottomRightContainer = $("#bottom-right-container:first-child").attr("id");
console.log(topRightContainer);
控制台的输出是&#34;右上角容器&#34;
答案 0 :(得分:0)
这将为您提供正确的答案,即#top-right-container
var topRightContainer = $("#top-right-container div:first-child").attr("id");
alert(topRightContainer);