Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.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
Html 如何在字符串中插入标记以替换字母并对其进行自定义?_Html_Reactjs_Tags_Components - Fatal编程技术网

Html 如何在字符串中插入标记以替换字母并对其进行自定义?

Html 如何在字符串中插入标记以替换字母并对其进行自定义?,html,reactjs,tags,components,Html,Reactjs,Tags,Components,我想插入一个html标记,以便自定义单个字母。问题是结果是[object]。 怎么了 const getAndChangeCustomI = (content) => { let iIndex = content.indexOf("i"); let insert = []; insert.push(<span className="customLetterI" > I</span>) conte

我想插入一个html标记,以便自定义单个字母。问题是结果是[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;
}
const getAndChangeCustomI=(内容)=>{
设iIndex=content.indexOf(“i”);
插入=[];
插入.推(I)
content.slice(0,iIndex)+content.slice(iIndex+1);
如果(iIndex>0){
返回content.substring(0,iIndex)+insert[0]+content.substr(iIndex);
}
返回insert+内容;
}

您正在尝试将字符串与节点数组连接起来。应将返回值构造为节点,以保留跨度:

return <>{content.substring(0, iIndex)}{insert[0]}{content.substr(iIndex)}</>;
返回{content.substring(0,iIndex)}{insert[0]}{content.substr(iIndex)};

Hi,它也不起作用。我只得到了[object object],字符串中没有任何内容。你确定没有其他内容影响它吗?我可以用我上面的建议运行你的代码,它工作得很好:嗨,你的代码工作得很好,问题是内容标签中的“危险的SetinerHTML”。谢谢。