从页面中删除特定的空<p> </p>

时间:2013-03-19 06:13:15

标签: jquery

美好的一天

我正在使用wordpress,我在我的div元素下面有一个令人烦恼的空段落标记

现在页面上有许多其他空段落,我不想删除,只有这一段......

这是HTML:

<div class="wpcol-one-quarter">
   <p><a rel="slb_group[803] slb slb_internal" href="http://asksomeone.co.za/wp-content/uploads/2013/03/boltmaster1.jpg"><img alt="boltmaster" src="http://asksomeone.co.za/wp-content/uploads/2013/03/boltmaster1-300x200.jpg" width="180" height="120" /></a></p>
</div>

<p>&nbsp;</p> <!--The culprit!!-->

<div class="wpcol-one-quarter">
   <p><a rel="slb_group[803] slb slb_internal" href="http://asksomeone.co.za/wp-content/uploads/2013/03/trackmaster.jpg"><img alt="trackmaster" src="http://asksomeone.co.za/wp-content/uploads/2013/03/trackmaster-300x200.jpg" width="180" height="120" /></a></p>
</div>
<div class="wpcol-one-quarter">
   <p><a rel="slb_group[803] slb slb_internal" href="http://asksomeone.co.za/wp-content/uploads/2013/03/multimaster.jpg"><img alt="multimaster" src="http://asksomeone.co.za/wp-content/uploads/2013/03/multimaster-300x200.jpg" width="180" height="120" /></a></p>
</div>
<div class="wpcol-one-quarter wpcol-last">
   <p><a rel="slb_group[803] slb slb_internal" href="http://asksomeone.co.za/wp-content/uploads/2013/03/facemaster1.4.jpg"><img alt="facemaster1.4" src="http://asksomeone.co.za/wp-content/uploads/2013/03/facemaster1.4-300x200.jpg" width="180" height="120" /></a></p>
</div><div class="wpcol-divider"></div>

现在我计划通过以下方式删除它:

            <script type="text/javascript">
                jQuery(function($){
                    var pathname = window.location.pathname;
                    if(pathname == '/803-2'){
                        $('div.wpcol-one-quarter').next(){

                            $('p').each(function() {
                                var $this = $(this);
                                if($this.html().replace(/\s|&nbsp;/g, '').length == 0)
                                    $this.remove();
                            });
                        }
                    }
                });
            </script>

为什么不起作用?

谢谢

4 个答案:

答案 0 :(得分:2)

试试这个:

<script>
$(document).ready(function(){
  $(".wpcol-one-quarter").next("p:empty").remove();
});
</script>

答案 1 :(得分:0)

试试这个

$('div.wpcol-one-quarter').next('p').filter(function () { return $.trim($(this).html()) == "" }).remove();

答案 2 :(得分:0)

试试这个:

   jQuery(function($){

        var pathname = window.location.pathname;

        if(pathname == '/803-2'){               

                  $("div.wpcol-one-quarter").next('p:empty').remove();

                  if($("div.wpcol-one-quarter").next('p').html()=="&nbsp;") {
                   $("div.wpcol-one-quarter").next('p').remove();
                  }

        }
    });

答案 3 :(得分:0)

它不会变得更短,你将在js脚本代码上调整大小,从长远来看,这会使你的东西加载更快。

$('p:contains("\xa0")').remove(); //to find all issues

$('.wpcol-one-quarter').next('p:contains("\xa0")').remove(); //to remove specific issue