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

javascript - Finding paragraphs with only   and removing the p tag - Stack Overflow

programmeradmin5浏览0评论

Im trying to find all p tags with inline style attrs style="text-align:center;" that contain just   and nothing else. And then removing the entire p for each found.

There are other p tags with the same attr that contain more than just nbsp which I want to keep, Im just stuck on finding the ones with a space only. Stupid wordpress tinyMCE editor, drives me nuts.

This is the base of what i was working on doing it with. just outputs all the text-align center p tags.

var p = $('.entry p[style="text-align: center;"]');
p.each(function() { 
  console.log(p);
});

Stupid little thing, i dont want to spend anymore time trying to figure out what im doing wrong. Here's an example page that has the stuff im working with. /

Thanks

Im trying to find all p tags with inline style attrs style="text-align:center;" that contain just   and nothing else. And then removing the entire p for each found.

There are other p tags with the same attr that contain more than just nbsp which I want to keep, Im just stuck on finding the ones with a space only. Stupid wordpress tinyMCE editor, drives me nuts.

This is the base of what i was working on doing it with. just outputs all the text-align center p tags.

var p = $('.entry p[style="text-align: center;"]');
p.each(function() { 
  console.log(p);
});

Stupid little thing, i dont want to spend anymore time trying to figure out what im doing wrong. Here's an example page that has the stuff im working with. http://www.drinkinginamerica./page/14/

Thanks

Share Improve this question edited May 15, 2011 at 3:37 bluepnume 17.2k8 gold badges41 silver badges48 bronze badges asked May 15, 2011 at 3:30 jaredwillijaredwilli 12.4k6 gold badges44 silver badges42 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You can do this with the remove() function:

$('.entry p').each(function() {
    var $p = $(this),
        txt = $p.html();
    if (txt==' ') {
        $p.remove();   
    }
});

See http://jsfiddle/nrabinowitz/LJHyA/ for a working example. This assumes a pretty strict case in which the only thing in the p tag is the   entity.


Too slow - @mu wins.

I think you want this:

$('.entry p[style="text-align: center;"]').each(function() {
    var $this = $(this);
    if($this.html().match(/^\s* \s*$/))
        $this.remove();
});

This is a bit sensitive to the precise style attribute but somewhat forgiving on how the the   is formatting inside the paragraphs.

I think something a bit looser might serve you better:

$('.entry p').each(function() {
    var $this = $(this);
    if($this.css('text-align') == 'center'
    && $this.html().match(/^\s* \s*$/))
        $this.remove();
});

http://jsfiddle/ambiguous/nMKnR/

发布评论

评论列表(0)

  1. 暂无评论