在jQuery中查找克隆元素的子元素

时间:2014-05-14 17:54:51

标签: javascript jquery clone children

我不知道为什么我会坚持这个但是我!尝试克隆div,然后使用jQuery中的children修改其内容。我在这里遗漏了一些东西,因为它没有像我期望的那样工作。请参阅小提琴:http://jsfiddle.net/v7A2T/

Javascript(jQuery):

$test = $('#clone-container').clone().appendTo('#append');
$test.children('h2').text('my clone');  // works
$test.children('.remove-row').remove(); // doesn't work

HTML:

<div id="clone-container" class="hidden">
    <h2>Location name</h2>
    <div class="table-responsive">
        <table class="table">
            <thead>
                <th>one</th><th>two</th><th>three</th>
                <th>four</th><th>five</th><th>six</th>
            </thead>
            <tbody>
                <tr class="remove-row"><td colspan="6">Remove this text from clone</td></tr>            
            </tbody>
        </table>
    </div> <!-- .table-responsive -->
</div>  
<div id="append"></div>

2 个答案:

答案 0 :(得分:6)

.remove-row不是克隆元素的直接子元素。替换这个:

$test.children('.remove-row').remove();

用这个:

$test.find('.remove-row').remove();

Fiddle

答案 1 :(得分:0)

让我们说我克隆了一个TR,我想在TR的TD中搜索一个按钮然后我可以这样做。按钮的类别为“绿色”。

var tr = $(this).closest("tr").clone();
tr.children().find('button.green').hide(); // or show