通过Jquery从Hidden Div中删除一个项目

时间:2013-11-14 06:24:57

标签: jquery

我有一个表格结构

   <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();

3 个答案:

答案 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();