如何根据以下情况获取图像的ID
JQUERY
$(".favoritar").click(function (data) {
// Here I want recover the previous IMG ID, that in this case should be
// DannaWhite, but the following code returns <img src="../../fotos_teste/05.PNG" id="DannaWhite" width="50" height="50" />
alert($(this).parent().closest(".bxFotoUsuarioLateral").html());
});
HTML
<div class="modal-header" style="background-color: #0F4265 !important;">
<div class="bxFotoUsuarioLateral">
<img src="../../fotos_teste/05.PNG" id="DannaWhite" width="50" height="50" />
</div>
<div class="icon-star"></div>
<img src="../../Images/favorito.png" class="favoritar" id="favoritarPost1" style="float: right; cursor: pointer;" />
<h4 class="modal-title" style="margin-left: 70px;">Cancelamento de Contrato <span id="idImovelAlteraStatus"></span></h4>
<span class="txtSample05" style="text-transform: none; margin-left: 40px;">Há 2 horas</span>
</div>
答案 0 :(得分:1)
如果它只有一张图片:
var id = $(this).closest('div.modal-header')
.find('.bxFotoUsuarioLateral > img').prop('id');
如果你想要一组id,无论图像数量多少:
var ids = $(this).closest('div.modal-header')
.find('.bxFotoUsuarioLateral > img')
.map(function(){
return this.id;
}).get();
答案 1 :(得分:0)
您可以使用:
$(this).closest(".modal-header").find(".bxFotoUsuarioLateral img").attr("id");
或
$(this).parents(".modal-header").find(".bxFotoUsuarioLateral img").attr("id");
<强> Demo 强>
答案 2 :(得分:0)
$(".favoritar").click(function (data) {
// Here I want recover the previous IMG ID, that in this case should be
// DannaWhite, but the following code returns undefined
alert( $(this).parents(".modal-header").find(".bxFotoUsuarioLateral img").attr("id"))
});