remove br tags and leave all other tags
I just wa开发者_C百科nt to remove the br tags.
'<div class="temp">some content<br>and more<br><a href="#"> and more content</a></div>'
$('.temp br').contents().unwrap();
why doesn't this work.
I also tried this as I saw it on stackoverflow but didn't work either. What am I doing wrong with both of these.
$('.temp').contents().filter(function() {
return this.nodeType == 3;
}).filter('br').remove();
That doesn't work because it's wrong. Try this:
$('.temp br').remove();
Demo.
To replace them with spaces:
$('.temp br').replaceWith(" ");
Demo.
Here is a working solution: http://jsfiddle.net/p6Mzu/. There is also a CSS only solution that is commented out in the link, as this only removes them. The jQuery replaces the <br>
with
.
jQuery:
$(".temp br").replaceWith(" ");
CSS:
.temp br { display: none; }
$('.temp br').before(' ').remove();
http://api.jquery.com/before/
http://api.jquery.com/remove/
精彩评论