Javascript 删除行中额外元素的函数

Javascript 删除行中额外元素的函数,javascript,jquery,Javascript,Jquery,如何修改此方法 $.fn.removeExtraBR= function() { var $this = $(this), $children = $this.children(), $el; if ($children.size() > 0) { $children.each(function(i, el) { $el = $(el); if($el.is('br') &am

如何修改此方法

$.fn.removeExtraBR= function() {
    var $this = $(this),
        $children = $this.children(),
        $el;

    if ($children.size() > 0) {
        $children.each(function(i, el) {
            $el = $(el);
            if($el.is('br') && $el.next().is('br')) {
                $el.remove();
            }
            if ($el.children().size() > 0) {
                $el.removeExtraBR();
            }
        });
    }
};
因此它接受两个参数:要检查的元素的名称和行中的最大值

例如:

html:

结果html:

<div id='test'>
  <p></p>
  <p></p>
  <br>
  <div>
    <p></p>
    <p></p>
  </div>
</div>


试试这个

$.fn.removeExtraBR= function(element, size) {
    var parentDiv = $(this);
        /*children = $this.children(),
        el;*/


        $(this).children(element).each(function(i, el) {
            if (parentDiv.children(element).size() > size) {
                $(this).remove();
            }
        });
    if($(this).children('div').size()>0)
        $(this).children('div').removeExtraBR('p', 2);

};

$('#test').removeExtraBR('p', 2);
此处演示如下:

$.fn.maxElements = function(selector, limit) {
    if ($(this).children(selector).size() > limit) {
        $(this).children(selector).each(function(i, el) {
            if(i >= limit) {
                $(el).remove();
            }
        });
    }
};
演示:

这里是一个尝试:

$.fn.removeExtraNodes = function (nodeType, maxCount) {
    var $this = $(this),
        $children = $this.children(),
        $el;

    var counter = 0;
    if ($children.size() > 0) {
        $children.each(function (i, el) {
            $el = $(el);
            if ($el.children().size() > 0) {
                $el.removeExtraNodes(nodeType, maxCount);
            }
            if ($el.is(nodeType)) {
                if (counter >= maxCount) {
                    $el.remove();
                } else {
                    counter++;
                }
            } else {
                counter = 0;
            }
        });
    }
};

$.fn.maxElements = function(selector, limit) {
    if ($(this).children(selector).size() > limit) {
        $(this).children(selector).each(function(i, el) {
            if(i >= limit) {
                $(el).remove();
            }
        });
    }
};
$.fn.removeExtraNodes = function (nodeType, maxCount) {
    var $this = $(this),
        $children = $this.children(),
        $el;

    var counter = 0;
    if ($children.size() > 0) {
        $children.each(function (i, el) {
            $el = $(el);
            if ($el.children().size() > 0) {
                $el.removeExtraNodes(nodeType, maxCount);
            }
            if ($el.is(nodeType)) {
                if (counter >= maxCount) {
                    $el.remove();
                } else {
                    counter++;
                }
            } else {
                counter = 0;
            }
        });
    }
};