JQuery: select paragraphs that only contain “ ” [duplicate]

ε祈祈猫儿з 提交于 2019-12-23 23:51:50

问题


Possible Duplicate:
Remove elements with only a   space using jQuery

Wordpress does this thing with adding <p>&nbsp;</p> when doing a line-break in the backend.

I wonder if it is possible to select those p's somehow with jquery.

I guess empty() wouldn't work since it isn't really empty but contains a &nbsp;. But contains() wouldn't probably work either since a lot of other paragraphs also contain a space, right?

I only want to select those paragpahs that have only this " " inside.

UPDATE

I should have mentioned that I don't want to remove the ps but add a class to it.


回答1:


Add this to your functions.php file if you have access to the theme files.

function user_content_replace($content) {
    return str_replace('<p>&nbsp;</p>','<p class="example">&nbsp;</p>',$content);
}
add_filter('the_content','user_content_replace', 99);

I made it insert a class instead of removing.




回答2:


This would work to select the paragraphs in question:

$("p").filter(function() {
    return $.trim($(this).html()) == '&nbsp;';
});



回答3:


You can do something like this

$(document).ready(function(){ 
            $("p").each(function(){
                if($(this).html()=="&nbsp;") {
                    alert($(this).attr('id'));
                }   
            });
        });

And the HTML I used was

<p id="test" class="les">&nbsp;</p>
<p class="les" id="test1">aaaa &nbsp;</p>

So it should alert only p with id test

Hope this helps



来源:https://stackoverflow.com/questions/14703924/jquery-select-paragraphs-that-only-contain-nbsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!