如何删除匿名节点?

时间:2014-06-06 15:28:34

标签: javascript jquery

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

fiddle

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>

我的数据库操作:

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。

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

1 个答案:

答案 0 :(得分:1)

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

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

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