将 JSON \n 转换为 HTML <br> 标签
Convert JSON \n to HTML <br> tag
我一直在使用 YouTube A.P.I 开发网站。
在 JSON 文件的 description
标记内是换行符 \n
我需要将这些标签转换为 HTML 格式
VideoDescriptions.push(item.snippet.description);
["Example description\nPlease click the link", "Another example description\nMore info"]
编辑:
此问题与链接文章不重复,因为:
- 它正在使用 YouTube API 检索数据
- 有必要从数组而不是字符串进行编辑(因为
文章中描述)
- 任一问题的答案都可能导致不同的结果,
可能不适用
您可以使用 javascript 替换功能,并根据需要替换项目:
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.split(search).join(replacement);
};
var test = ["yes, this is my example\n", "goodbye"];
console.log(test[0].replaceAll('\n', '<br>'))
您可以简单地在 javascript:
中使用字符串 replace
var items = ["Example description\nPlease click the link\n\n", "Another example description\nMore info"];
console.clear();
var changed = items.map(i => i.replace(/\n/g, '<br />')).join('');
var div = document.querySelector("#test");
div.innerHTML = changed;
<div id="test"></div>
您可能应该从每一行创建单独的节点,而不是使用 <br>
,因为它允许更大的灵活性。你可以这样做:
var description = ["Example description\nPlease click the link", "Another example description\nMore info"];
var fragment = description.reduce(function (frag, line) {
var parts = line.split('\n');
var container = document.createElement('div');
var firstLine = document.createElement('p');
var secondLine = document.createElement('a');
firstLine.textContent = parts[0];
secondLine.textContent = parts[1];
secondLine.href = '#';
container.appendChild(firstLine);
container.appendChild(secondLine);
frag.appendChild(container);
return frag;
}, document.createDocumentFragment());
document.querySelector('.parent').appendChild(fragment);
<div class="parent"></div>
这里我们获取描述并将其简化为 documentFragment 包含一个容器元素,描述数组中每个项目有两个子元素。
使用 textContent 之类的方法可以防止 XSS 攻击向量(即使描述在您的控制范围内,也不要为了好玩而创建可能的 XSS 漏洞)。
有趣的是,如果您通过 .innerText 设置标签的内容,而不是 innerHTML。
所有的 \n 字符都会为您转换成 < br > 标签。
例如:
document.getElementsByTagName('h1')[0].innerText = 'New text with a \nLine break.'
我一直在使用 YouTube A.P.I 开发网站。
在 JSON 文件的 description
标记内是换行符 \n
我需要将这些标签转换为 HTML 格式
VideoDescriptions.push(item.snippet.description);
["Example description\nPlease click the link", "Another example description\nMore info"]
编辑:
此问题与链接文章不重复,因为:
- 它正在使用 YouTube API 检索数据
- 有必要从数组而不是字符串进行编辑(因为 文章中描述)
- 任一问题的答案都可能导致不同的结果, 可能不适用
您可以使用 javascript 替换功能,并根据需要替换项目:
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.split(search).join(replacement);
};
var test = ["yes, this is my example\n", "goodbye"];
console.log(test[0].replaceAll('\n', '<br>'))
您可以简单地在 javascript:
中使用字符串replace
var items = ["Example description\nPlease click the link\n\n", "Another example description\nMore info"];
console.clear();
var changed = items.map(i => i.replace(/\n/g, '<br />')).join('');
var div = document.querySelector("#test");
div.innerHTML = changed;
<div id="test"></div>
您可能应该从每一行创建单独的节点,而不是使用 <br>
,因为它允许更大的灵活性。你可以这样做:
var description = ["Example description\nPlease click the link", "Another example description\nMore info"];
var fragment = description.reduce(function (frag, line) {
var parts = line.split('\n');
var container = document.createElement('div');
var firstLine = document.createElement('p');
var secondLine = document.createElement('a');
firstLine.textContent = parts[0];
secondLine.textContent = parts[1];
secondLine.href = '#';
container.appendChild(firstLine);
container.appendChild(secondLine);
frag.appendChild(container);
return frag;
}, document.createDocumentFragment());
document.querySelector('.parent').appendChild(fragment);
<div class="parent"></div>
这里我们获取描述并将其简化为 documentFragment 包含一个容器元素,描述数组中每个项目有两个子元素。
使用 textContent 之类的方法可以防止 XSS 攻击向量(即使描述在您的控制范围内,也不要为了好玩而创建可能的 XSS 漏洞)。
有趣的是,如果您通过 .innerText 设置标签的内容,而不是 innerHTML。 所有的 \n 字符都会为您转换成 < br > 标签。
例如:
document.getElementsByTagName('h1')[0].innerText = 'New text with a \nLine break.'