Javascript 如何将\n转换为<;br>;在提取JSON数据时标记?
我的JSON输出当前如下所示:Javascript 如何将\n转换为<;br>;在提取JSON数据时标记?,javascript,jquery,json,Javascript,Jquery,Json,我的JSON输出当前如下所示: "description":"MUSIC VIDEO - 7:13\n\nCREDITS\n\nLabel: Black Pain Records\nProduction Company: Idyll Films 我有一点jQuery要获取并作为文本输入: $.getJSON("http://vimeo.com/api/oembed.json?url=http://vimeo.com/"+strip2+"&callback=?",
"description":"MUSIC VIDEO - 7:13\n\nCREDITS\n\nLabel: Black Pain Records\nProduction Company: Idyll Films
我有一点jQuery要获取并作为文本输入:
$.getJSON("http://vimeo.com/api/oembed.json?url=http://vimeo.com/"+strip2+"&callback=?",
function(json){
$('.creditText').text(json.description);
});
strip2是我的vimeo id。目前输出都是一行,我想知道\n将在哪里转换为标签,这可能吗?您可以这样做
json.description.replace(/\n/g, '<br/>')
json.description.replace(/\n/g,
)
为此使用java脚本的替换函数
$.getJSON("http://vimeo.com/api/oembed.json?url=http://vimeo.com/"+strip2+"&callback=?",
function(json){
$('.creditText').text(json.description.replace('\n','<br>'));
});
$.getJSON(“http://vimeo.com/api/oembed.json?url=http://vimeo.com/“+strip2+”&回调=?”,
函数(json){
$('.creditText').text(json.description.replace('\n','
'));
});
尝试替换行$('.creditText').text(json.description)代码>带有:
$('.creditText').html(json.description.replace(/\n/g, "<br/>");
$('.creditText').html(json.description.replace(/\n/g,“
”);
请注意,您需要将text()
更改为html()
,以便
元素将作为html插入并显示在页面上
请参见您需要一个正则表达式,否则只会替换第一个出现的表达式。我们可以使用prepend或append代替html吗?这两个函数的作用相同