最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Use jQuery to remove a <p> with an empty <span> tag - Stack Overflow

programmeradmin2浏览0评论

I'm working on a Wordpress build using the Tubepress plug-in. This plug-in inserts an unwanted <p> above the video content. I'd like to remove this tag using jQuery.

Here's the code I'd like to remove:

<p><span id="more-76"></span></p>

The span id is auto-generated and will be different for each piece of content. This code is placed in the following div.

<div class="work1alt">
   <p><span id="more-76"></span></p>
</div>

Thanks in advance for help.

I'm working on a Wordpress build using the Tubepress plug-in. This plug-in inserts an unwanted <p> above the video content. I'd like to remove this tag using jQuery.

Here's the code I'd like to remove:

<p><span id="more-76"></span></p>

The span id is auto-generated and will be different for each piece of content. This code is placed in the following div.

<div class="work1alt">
   <p><span id="more-76"></span></p>
</div>

Thanks in advance for help.

Share Improve this question edited Jan 28, 2013 at 18:48 eykanal 27.1k19 gold badges85 silver badges114 bronze badges asked Nov 29, 2010 at 0:31 scout75scout75 371 silver badge8 bronze badges 1
  • I realized about an hour after I posted this that the "MORE" tag within the editor was inserting this text at the top of the_content(). I no longer needed to use the "MORE" tag, so this solved the problem. I apologize for not posting this sooner, but appreciate the different approaches listed below. I'll now know what to do if I run into this problem in the future. Thank you. – scout75 Commented Nov 30, 2010 at 4:20
Add a ment  | 

4 Answers 4

Reset to default 8

Like this:

$('span:empty:only-child').parent('p').remove();

This will be extremely fast, but will not select <span>s with whitespace in them.

$('div.work1alt').find('p span:empty').parent().remove();

The first if condition checks if there is no children or content I believe. The second checks if the span contains empty text and will return only text within the span, but not it's children.

Not as elegant and probably not as fast as slak's solution, but is very explicit.

 $('div.work1alt > p span').each(function(){
    if($.trim($(this).text()) === ""){
       $(this).parent().remove();
    }
 });

EDIT: forgot to put the '===""' in the ifs (doh!)

depends on what else stands on your page. Maybe you should try this like that $('p').remove();

发布评论

评论列表(0)

  1. 暂无评论