Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将\n转换为<;br>;在提取JSON数据时标记?_Javascript_Jquery_Json - Fatal编程技术网

Javascript 如何将\n转换为<;br>;在提取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=?",

我的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=?",
                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吗?这两个函数的作用相同