使用 TSX 语法时如何在 HTML span 中使用 href?

我是使用 Typescript 和 React 的新手,如果这是一个新手问题,我很抱歉。在我的代码中,我有以下行:


<p><span href="#" id="nsTool" style={{color:"white", width: "50px"}}> Namespace </span></p>


当代码仅使用 Javascript/React 时,这最初工作正常,但当我将文件转换为 .tsx(Typescript)时,出现以下错误:


Type '{ children: string[]; href: string; id: string; style: { color: string; width: string; }; }' is not assignable to type 'DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>'.

  Property 'href' does not exist on type 'DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>'.

这是否意味着我不能在 span 元素中使用 href ?我该如何解决这个问题?


眼眸繁星
浏览 74回答 2
2回答

手掌心

HTMLspan元素只允许全局属性。因此该href属性不会产生影响span,因此您会收到 Typescript 错误。注意:该href属性通常只能在anchor element

喵喵时光机

<span>标签没有href属性。您可以像这样附加带有锚标记的 span 标记:<span>&nbsp; &nbsp; :&nbsp;&nbsp;&nbsp; &nbsp; <a href="#">&nbsp; &nbsp; &nbsp; &nbsp; Namespace&nbsp; &nbsp; </a></span>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5