开发者

remove br tags and leave all other tags

开发者 https://www.devze.com 2023-03-26 21:45 出处:网络
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>\'

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("&nbsp;");

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 &nbsp;.

jQuery:

$(".temp br").replaceWith("&nbsp;");

CSS:

.temp br { display: none; }


$('.temp br').before('&nbsp;').remove();

http://api.jquery.com/before/
http://api.jquery.com/remove/

0

精彩评论

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