[英]Selecting an element within an element with different ID names or class name in jQuery
我有一个有表的无序列表。 该列表还使用图像,当单击该图像时应隐藏该表。 我有以下jQuery代码,但无法正常工作:
$(".glyphicon-remove").click(function () {
var tblId = $(this).attr("id");
// alert(tblId);
var ans = confirm("Are you sure you want to remove this table?");
if(ans == true)
$("#" + tblId).hide();
});
并且使用的列表如下:
<ul class="sortable">
<li class="ui-state-default" id="first"><span style="float:right;"class="glyphicon glyphicon-remove"></span><br/>
<table class="rounded-corner">
<thead>
<tr>
<th></th>
<th>Product</th>
<th>Details</th>
<th>Price</th>
<th>Date</th>
<th>Category</th>
<th>User</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="12">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut.</td>
</tr>
</tfoot>
<tbody>
<tr class="odd">
<td><input type="checkbox" name="" /></td>
<td>Box Software</td>
<td>Lorem ipsum dolor sit amet consectetur</td>
<td>45$</td>
<td>10/04/2011</td>
<td>web design</td>
<td>Alex</td>
<td><a href="#"><img src="images/edit.png" alt="" title="" border="0" /></a></td>
<td><a href="#"><img src="images/trash.gif" alt="" title="" border="0" /></a></td>
</tr>
<tr class="even">
<td><input type="checkbox" name="" /></td>
<td>Trial Software</td>
<td>Lorem ipsum dolor sit amet consectetur</td>
<td>155$</td>
<td>12/04/2011</td>
<td>web design</td>
<td>Carrina</td>
<td><a href="#"><img src="images/edit.png" alt="" title="" border="0" /></a></td>
<td><a href="#"><img src="images/trash.gif" alt="" title="" border="0" /></a></td>
</tr>
<tr class="odd">
<td><input type="checkbox" name="" /></td>
<td>Hosting Pack</td>
<td>Lorem ipsum dolor sit amet consectetur</td>
<td>45$</td>
<td>10/04/2011</td>
<td>web design</td>
<td>Alex</td>
<td><a href="#"><img src="images/edit.png" alt="" title="" border="0" /></a></td>
<td><a href="#"><img src="images/trash.gif" alt="" title="" border="0" /></a></td
</tr>
<tr class="even">
<td><input type="checkbox" name="" /></td>
<td>Duo Software</td>
<td>Lorem ipsum dolor sit amet consectetur</td>
<td>745$</td>
<td>10/04/2011</td>
<td>web design</td>
<td>Alex</td>
<td><a href="#"><img src="images/edit.png" alt="" title="" border="0" /></a></td>
<td><a href="#"><img src="images/trash.gif" alt="" title="" border="0" /></a></td
</tr>
<tr class="odd">
<td><input type="checkbox" name="" /></td>
<td>Alavasti Software</td>
<td>Lorem ipsum dolor sit amet consectetur</td>
<td>45$</td>
<td>10/04/2011</td>
<td>web design</td>
<td>John</td>
<td><a href="#"><img src="images/edit.png" alt="" title="" border="0" /></a></td>
<td><a href="#"><img src="images/trash.gif" alt="" title="" border="0" /></a></td
</tr>
<tr class="even">
<td><input type="checkbox" name="" /></td>
<td>Box Software</td>
<td>Lorem ipsum dolor sit amet consectetur</td>
<td>45$</td>
<td>10/04/2011</td>
<td>web design</td>
<td>Doe</td>
<td><a href="#"><img src="images/edit.png" alt="" title="" border="0" /></a></td>
<td><a href="#"><img src="images/trash.gif" alt="" title="" border="0" /></a></td
</tr>
</tbody>
</table>
</li>
<li class="ui-state-default" id="second">2</li>
<li class="ui-state-default" id="third">3</li>
<li class="ui-state-default" id="fourth">4</li>
<li class="ui-state-default" id="fifth">5</li>
<li class="ui-state-default" id="sixth">6</li>
<li class="ui-state-default" id="seventh">7</li>
<li class="ui-state-default" id="eighth">8</li>
<li class="ui-state-default" id="ninth">9</li>
<li class="ui-state-default" id="tenth">10</li>
</ul>
请帮帮我。
您可以使用:
var tblId = $(this).parent().attr("id");
代替:
var tblId = $(this).attr("id");
因为你想要得到的id
的li
元素不span
带班glyphicon-remove
尝试这个:
$(".glyphicon-remove").click(function () {
var tblId = $(this).attr("id");
// alert(tblId);
var ans = confirm("Are you sure you want to remove this table?");
if(ans == true)
$(this).siblings('table').hide();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.