如何移除匿名节点

how to remove the anonymous nodes?

本文关键字:节点 何移      更新时间:2023-09-26

帮助请删除匿名文本节点。

小提琴html:

<div class="cell image_outer">
    <label class="label">Фото</label>
    <div class="box_common_outer">
        <div class="box_common">
            <div class="box_common_inner">
                <img src="/media/cache/61/ad/61ad85ae26431c6fa3de2438e73bd7d2.jpg" width="83" height="83">
            </div>
        </div>
    </div>  
    На данный момент: 
    <a href="/media/userprofile/path_glory_photo/n_2.jpg">userprofile/path_glory_photo/n_2.jpg</a> 
    <input id="path_glory_photo-clear_id" name="path_glory_photo-clear" type="checkbox"> 
    <label for="path_glory_photo-clear_id">Очистить</label>
    <br>
    Изменить: 
    <input id="id_path_glory_photo" name="path_glory_photo" type="file">
</div>

应该能够:

<div class="cell image_outer">
    <label class="label">Фото</label>
    <div class="box_common_outer">
        <div class="box_common">
            <div class="box_common_inner">
                <img src="/media/cache/61/ad/61ad85ae26431c6fa3de2438e73bd7d2.jpg" width="83" height="83">
            </div>
        </div>
    </div>  
    <input id="path_glory_photo-clear_id" name="path_glory_photo-clear" type="checkbox"> 
    <label for="path_glory_photo-clear_id">Очистить</label>
    <input id="id_path_glory_photo" name="path_glory_photo" type="file">
</div>

my database operations:

var label = $('.image_outer .label');
var thumb = $('.image_outer box_common_outer');
var label2 = $('.image_outer label[for="path_glory_photo-clear_id"]');
var photo_input = $('#id_path_glory_photo');
var checkbox = $('#path_glory_photo-clear_id');
$('.image_outer').empty().append(label).append(thumb).append(label2).append(photo_input).append(checkbox);

结果输出:label, label2, photo_input, checkbox。

问题是没有出现拇指,没有嵌套元素

删除.empty()。你正在做的是清除'image_outer删除它里面的所有元素,而不仅仅是文本元素。您需要找到文本元素并删除它们…

http://jsfiddle.net/3SDGZ/1/

 $(".image_outer").contents().filter(function() {
      return this.nodeType == 3;
 }).remove();