开发者

Finding paragraphs with only   and removing the p tag

开发者 https://www.devze.com 2023-03-06 12:12 出处:网络
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.

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.drinkinginamer开发者_Go百科ica.com/page/14/

Thanks


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.net/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.net/ambiguous/nMKnR/

0

精彩评论

暂无评论...
验证码 换一张
取 消