【问题标题】:Finding child elements of a cloned element in jQuery在jQuery中查找克隆元素的子元素
【发布时间】:2014-05-14 17:54:50
【问题描述】:

我不知道为什么我会被困在这个问题上,但我就是!尝试克隆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>

【问题讨论】:

  • find vs children,旧的 jQuery 初始陷阱。

标签: javascript jquery clone children


【解决方案1】:

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

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

用这个:

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

Fiddle

【讨论】:

    【解决方案2】:

    假设我已经克隆了一个 TR,我想在 TR 的 TD 中搜索一个按钮,然后我可以这样做。该按钮具有“绿色”类。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-31
      • 2011-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多