使用Jquery在DOM中查找元素并删除[重复]

时间:2022-12-08 00:01:22

Possible Duplicate:
Clear 5 empty TDs with Jquery

可能重复:使用Jquery清除5个空TD

I want to search the DOM for any instance (might be multiple) where the DOM contains specifically

我想在DOM中搜索DOM包含的任何实例(可能是多个)

<td></td><td></td><td></td><td></td><td></td>

and remove it in Jquery. Any help?

并在Jquery中删除它。有帮助吗?

3 个解决方案

#1


1  

If you constant string then you can remove it from html,

如果你是常量字符串,那么你可以从html中删除它,

Live Demo

$('#tbl').html($('#tbl').html().replace('<td></td><td></td><td></td><td></td><td></td>', ''));

#2


0  

I'd suggest:

var rows = $('tr').filter(function(){
                var that = $(this),
                    len = that.find('td').length;
                return that.find('td:empty').length == len && len == 5;
            });

References:

#3


0  

This is new demo:

这是一个新的演示:

HTML:

<table>
<tr>
<td></td><td></td><td></td><td></td><td></td>
</tr>
</table>​

JS

$(function(){

    $.map($("td:empty"),function(elem,i){
        console.log(i);

        var length =5;
        var current = length;
        var $ptr = $(elem);

        while(current>=0){
            if ($ptr.next("td:empty").size() == 1){
                current = current -1;
                $ptr = $ptr.next("td:empty");

            }else{
                break;
            }   
        }
        if(current<= 1 ){ 
            $(elem).siblings("td:empty:lt("+length+")").remove();
            $(elem).remove();                                
        }

    });

});​

#1


1  

If you constant string then you can remove it from html,

如果你是常量字符串,那么你可以从html中删除它,

Live Demo

$('#tbl').html($('#tbl').html().replace('<td></td><td></td><td></td><td></td><td></td>', ''));

#2


0  

I'd suggest:

var rows = $('tr').filter(function(){
                var that = $(this),
                    len = that.find('td').length;
                return that.find('td:empty').length == len && len == 5;
            });

References:

#3


0  

This is new demo:

这是一个新的演示:

HTML:

<table>
<tr>
<td></td><td></td><td></td><td></td><td></td>
</tr>
</table>​

JS

$(function(){

    $.map($("td:empty"),function(elem,i){
        console.log(i);

        var length =5;
        var current = length;
        var $ptr = $(elem);

        while(current>=0){
            if ($ptr.next("td:empty").size() == 1){
                current = current -1;
                $ptr = $ptr.next("td:empty");

            }else{
                break;
            }   
        }
        if(current<= 1 ){ 
            $(elem).siblings("td:empty:lt("+length+")").remove();
            $(elem).remove();                                
        }

    });

});​