如何用<br/>标签替换标签标签中的空格?

时间:2016-11-11 10:23:13

标签: javascript jquery html

如何替换&lt; label&gt;中的所有空格?用&lt; br&gt;?标记 例如,我有:

Parameters `{
    "shoutcast.destination": "",
    "icecast2.public": false,
    "akamai.destinationServer": "",
    "shoutcast.icq": "",
    "facebook.eventSourceName": "",
    "timeToLive": 0,
    "password": "",
    "facebook.destType": "",
    "rtpWrap": false,
    "icecast2.name": "",
    "akamai.hostId": "",
    "icecast2.icq": "",
    "host": "",
    "connectionFlashVersion": "",
    "cupertino.renditions": "",
    "facebook.title": "",
    "debugPackets": false,
    "shoutcast.public": false,
    "shoutcast.aim": "",
    "profile": "",
    "icecast2.description": "",
    "sessionStatus": "",
    "shoutcast.metaname": "",
    "icecast2.genre": "",
    "facebook.destId": "",
    "queryString": "",
    "akamai.eventName": "",
    "version": "",
    "shoutcast.name": "",
    "sendStreamCloseCommands": false,
    "port": 0,
    "sendFCPublish": false,
    "icecast2.aim": "",
    "http.playlistCount": 0,
    "http.playlistTimeout": 0,
    "facebook.destName": "",
    "audioPort": "",
    "icecast2.metaname": "",
    "sourceStreamName": "",
    "connectionCode": "",
    "localBindAddress": "",
    "shoutcast.protocol": "",
    "debugLogChildren": false,
    "serverName": "",
    "adaptiveStreaming": false,
    "facebook.eventSourceType": "",
    "enabled": false,
    "shoutcast.irc": "",
    "sendReleaseStream": false,
    "shoutcast.url": "",
    "icecast2.irc": "",
    "facebook.description": "",
    "debugLog": false,
    "shoutcast.genre": "",
    "akamai.streamId": "",
    "akamai.hdNetwork": false,
    "adaptiveGroups": "",
    "saveFieldList": [
      ""
  ],
    "http.playlistAcrossSessions": false,
    "secureTokenSharedSecret": "",
    "http.fakePosts": false,
    "sendSSL": false,
    "sendOriginalTimecodes": false,
    "icecast2.destination": "",
    "facebook.accessToken": "",
    "userName": "",
    "facebook.privacy": "",
    "streamName": "",
    "removeDefaultAppInstance": false,
    "videoPort": "",
    "http.writerDebug": false,
    "icecast2.url": "",
    "akamai.sendToBackupServer": false,
    "destinationServer": "",
    "application": "",
    "entryName": "",
    "streamWaitTimeout": 0,
    "appInstance": "",
    "originalTimecodeThreshold": "",
    "autoStartTranscoder": false,
    "sanjose.representationId": ""
}` 

并将其更改为:

<label>Some text here</label>

我尝试使用此代码:

<label>Some<br>text<br>here</label>

但它没有用。

2 个答案:

答案 0 :(得分:2)

使用html()方法和回调函数。并且用String#replace方法替换所有空间使用正则表达式与全局falg。

&#13;
&#13;
$('label').html(function(i, oldHTML) {
  return oldHTML.replace(/\s+/g, '<br>');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Some text here</label>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你必须设置html,这可能会起作用:

$('.form-group label').html($('.form-group label').html().replace(' ', '<br>'));