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

标签 javascript jquery clone children

我不知道为什么我坚持这个,但我就是!尝试克隆一个 div,然后使用 jQuery 中的 children 修改其内容。我在这里遗漏了一些东西,因为它没有像我预期的那样工作。参见 fiddle :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>

最佳答案

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

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

用这个:

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

Fiddle

关于javascript - 在 jQuery 中查找克隆元素的子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23661750/

相关文章:

javascript - 为元素创建变量还是每次都选择它是否更有效?

javascript - 在 Angular JS 中格式化时间戳

jquery - 如何对其他div应用相同的效果(淡入/淡出)?

jquery - 尝试使用 setTimeout 来创建一个小的延迟,但它不起作用

java - 从 'getter' 方法返回值的最佳方法

Git 不会在后续克隆上克隆所有分支?

Java 深度克隆 - 如果可克隆,则克隆 ArrayList 中的每个元素,无论类型如何

javascript - 在 node.js 中,为什么会有一个 util.isArray 和一个 Array.isArray?

javascript - 如何从字符串中删除后缀?

JavaScript 对象和 jQuery