如何在字符串中插入标签以替换字母并对其进行自定义?
How to insert a tag in a string to replace a letter and to customize it?
我想插入一个 html 标签来自定义单个字母。问题是结果是 [object object]。
错误是什么?
const getAndChangeCustomI = (content) => {
let iIndex = content.indexOf("i");
let insert = [];
insert.push(<span className="customLetterI" > I</span>)
content.slice(0, iIndex) + content.slice(iIndex + 1);
if (iIndex > 0) {
return content.substring(0, iIndex) + insert[0] + content.substr(iIndex);
}
return insert + content;
}
您正在尝试将字符串与节点数组连接起来。您应该将 return 值构建为节点以保留跨度:
return <>{content.substring(0, iIndex)}{insert[0]}{content.substr(iIndex)}</>;
我想插入一个 html 标签来自定义单个字母。问题是结果是 [object object]。 错误是什么?
const getAndChangeCustomI = (content) => {
let iIndex = content.indexOf("i");
let insert = [];
insert.push(<span className="customLetterI" > I</span>)
content.slice(0, iIndex) + content.slice(iIndex + 1);
if (iIndex > 0) {
return content.substring(0, iIndex) + insert[0] + content.substr(iIndex);
}
return insert + content;
}
您正在尝试将字符串与节点数组连接起来。您应该将 return 值构建为节点以保留跨度:
return <>{content.substring(0, iIndex)}{insert[0]}{content.substr(iIndex)}</>;