替换 nbsp;<br/> [英] Replace nbsp; with <br/>

查看:47
本文介绍了替换 nbsp;<br/>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想把这种代码放在第二位.

由于可用宽度,我想用 <br/> 替换 &nbsp;.

这是我当前的代码,但它不起作用:

$( "&nbsp;" ).replaceWith( "<br/>" );

JSFiddle

解决方案

给你:

$('#social-links').html($('#social-links').html().replace(/&nbsp;/g,"<br/>"));

更新:

现在我在 3 年后再次考虑它,我会这样做:

const $socialLinks = $('#social-links');$socialLinks.html($socialLinks.html().split('&nbsp;').join('<br/>'));

它的性能比之前的代码好很多.

I want to include this kind of code to a second place.

<div id="social-links">
  <!-- Facebook -->
  <a href="https://www.facebook.com/name" target="_blank">
    <i class="fa fa-facebook-square fa-lg"></i>
    Facebook
  </a>
  &nbsp;

  <!-- Google+ -->
  <a href="https://plus.google.com/+name" target="_blank">
    <i class="fa fa-google-plus fa-lg"></i>
    Google+
  </a>
  &nbsp;
</div>

Because of the available width I want to replace the &nbsp; with <br/>.

This is my current code but it doesn't work:

$( "&nbsp;" ).replaceWith( "<br/>" );

JSFiddle

解决方案

here you go:

$('#social-links').html($('#social-links').html().replace(/&nbsp;/g,"<br/>"));

UPDATE:

Now that I think about it again after 3 years, I'd do it this way:

const $socialLinks = $('#social-links');
$socialLinks.html($socialLinks.html().split('&nbsp;').join('<br/>'));

It has much better performance than the previous code.

这篇关于替换 nbsp;&amp;lt;br/&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆