我有一个表格结构
<table id='dataTable'>
<tr>
<td class="dataLabel">
<div style="display:none" class="hours_logged">
<div class="time_item">
<div class="record_id">34</div>
<div class="time_logged">2</div>
<div class="desc">Another Time Added</div>
</div>
<div class="time_item">
<div class="record_id">37</div>
<div class="time_logged">3</div>
<div class="desc">New Time Added</div>
</div>
</div>
</td>
</tr>
</table>
我想删除包含record_id“37”
的div类“time_item”这就是我正在尝试的
$('table#dataTable div:contains("37")').parent().remove();
答案 0 :(得分:3)
您可以使用:has-selector或.has()
$('#dataTable div.time_item:has(div.record_id:contains(37))').remove()
$('#dataTable div.time_item').has('div.record_id:contains(37)').remove()
演示:Fiddle
更准确的解决方案是
$('#dataTable div.time_item').filter(function () {
return $.trim($(this).find('.record_id').text()) == '37';
}).remove()
演示:Fiddle
答案 1 :(得分:1)
$("table#dataTable div:contains('34')").parent().remove();
或
var containsVal = "34";
$('#dataTable div.time_item:has(div.record_id:contains('+ containsVal+'))').remove();
答案 2 :(得分:0)
如果您有ID,出于性能目的,最好在HTML中使用它们:
<div class="time_item">
<div id="record_37" class="record_id">37</div>
<div class="time_logged">3</div>
<div class="desc">New Time Added</div>
</div>
然后使用jQuery
$('#record_37').closest('.time_item').remove();