问题
Possible Duplicate:
Remove elements with only a space using jQuery
Wordpress does this thing with adding <p> </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
. 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 p
s 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> </p>','<p class="example"> </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()) == ' ';
});
回答3:
You can do something like this
$(document).ready(function(){
$("p").each(function(){
if($(this).html()==" ") {
alert($(this).attr('id'));
}
});
});
And the HTML I used was
<p id="test" class="les"> </p>
<p class="les" id="test1">aaaa </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