[英].parent().find('img').eq(0) Error
我想在“ a”標簽中捕獲“ img”,但不能輕松實現。 以下是我的html:
<table class="table table-bordered">
<thead>
<tr>
<td>Category Name</td>
<td>Status</td>
<td>Display Rank</td>
</tr>
</thead>
<tbody>
@foreach (var category in Model)
{
<tr>
<td style="width: 200px">
<span style="cursor: pointer">@category.Name</span>
</td>
<td style="width: 200px">
<div id="@category.ID">
@if (category.Status == 1)
{
<img src="~/img/ok-icon.png" />
<a href="javascript:void(0)" class="make-passive"><img val="@category.ID" src="~/img/Alarm-Error-icon.png" /></a>
}
else if (category.Status == 0)
{
<a href="javascript:void(0)" class="make-active">
<img val="@category.ID" src="~/img/Alarm-Tick-icon.png" />
</a>
<img src="~/img/Close-icon.png" />
}
</div>
</td>
<td style="width: 160px">
<a href="javascript:void(0)" class="take-up">
<img src="~/img/Arrows-Up-4-icon.png" />
</a><br />
<a href="javascript:void(0)" class="take-down">
<img src="~/img/Arrows-Down-4-icon.png" />
</a>
</td>
</tr>
}
</tbody>
</table>
而且,這是我的JS代碼:
<script type="text/javascript">
$(document).on('click', 'a.make-passive', function () {
var imgPassive = $(this).parent().find('img').eq(0);
var imgNo = imgPassive.attr('val');
$.ajax({
type: 'POST',
dataType: 'json',
url: "@Url.Action("DeactivateCategory", "Admin")/" + imgNo,
success: function (data) {
alert(data.Name + "has been successfully removed among active categories.");
$('#' + imgNo).html("trial by sword");
},
error: function (data) {
alert(data.Name + "cannot be removed among the other categories. An error occured.");
}
});
});
通過放置調試器,我意識到問題出在var imgPassive = $(this).parent().find('img').eq(0);
我在其他幾頁上成功使用了.parent().find()
方法,但看不到是什么使它在此頁上不起作用。
提前致謝!
您不需要$(this).parent().find('img').eq(0);
因為您要獲取的img
在clicked元素內。 您可以簡單地執行$(this).find('img').eq(0);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.