我正在使用ajax返回一些数据,我想将它作为第二个孩子插入div.问题是我如何在div中的最后一个元素之前插入一些html到div
$.ajax({
url: '@Url.Action("...", "..")',
traditional: true,
data: { coordinates: coordinates, selectedTypeIDs: selectedTypeIDs, pageNumber: pageNumber },
type: 'POST',
success: function (data) {
$('#listings').insertBefore(".last-child", data);
hideProgress();
},
error: function (xhr, textStatus, errorThrown) {
showErrorMessage("Something whacky happened", errorThrown);
}
Run Code Online (Sandbox Code Playgroud)
这是我的,div并在其中一个评论,显示我想把它放在哪里
<div id="listings">
<!--bunch of divs-->
<div>something</div>
<!--INSERT DATA HERE-->
<div class="black_box2" id="showMoreBar"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
我可以使用类名black_box2或更好,我分配给最后一个的id div,或者更快的方式
Alp*_*key 17
$('#listings').insertBefore(".last-child", data);会放在#listings之前.last-child(对于insertBefore,没有第二个参数).
你想要的是:
$("#showMoreBear").before(data); // put data before #showMoreBear
Run Code Online (Sandbox Code Playgroud)
要么
$(data).insertBefore('.last-child'); // put data before .last-child
Run Code Online (Sandbox Code Playgroud)
.before(content)将内容放在所选元素之前.
.insertBefore(target)将所选元素放在目标之前.