无法在 jQuery 手风琴的 DIV 中找到元素 ID

Cannot ID of element in DIV of jQuery accordion

我的手风琴看起来像这样:

 @for (int i = 0; i < Model.Count; i++)
        {
            <h3 id= '@Model[i].UserId'>@Model[i].UserName</h3>
            <div >
                @Html.HiddenFor(x => Model[i].UserId, new { @id = "txtId" + i })
                <table style="font-size:xx-small">
                    <tr>
                         <td>
                                            <img id='img + @Model[i].UserId' alt="" class="image" />
                                        </td>

这为图像元素建立了不同于 H3 header 的 ID。我可以这样得到 header 的 ID:

 $("#accordion > h3").click(function () {
        var currentID = $('.ui-accordion-header-active').attr('id');
        var divid = "#" + currentID;
        var imgId = $(divid).find(".image").attr("id");
        imgId = $('.ui-accordion-header-active .image').attr("id");
        alert(imgId);
    });

但是我就是获取不到图片元素的ID。我究竟做错了什么?在手风琴中单击此人之前,我不想加载任何图片。

您正在使用 .find 获取图像的 id,而 .find 试图获取引用的选择器的 children。因此,您需要再添加一个 .next 以从 div 中获得 children,如下所示,因为 .image 位于 div 内,紧挨着您的 header :

$("#accordion > h3").click(function () {
        var currentID = $('.ui-accordion-header-active').attr('id');
        var divid = "#" + currentID;
        var imgId = $(divid).next().find(".image").attr("id"); //.next here
        alert(imgId);
});

再次尝试在 header

中查找子 .image
imgId = $('.ui-accordion-header-active .image').attr("id");

而且我认为你在上面不再需要这个 event 所以我把它删除了..