[英]How to use href in HTML span when using TSX syntax?
I'm new to using Typescript and React so sorry if this is a novice question.我是使用 Typescript 和 React 的新手,如果这是一个新手问题,我很抱歉。 In my code I have the following line:
在我的代码中,我有以下几行:
<p><span href="#" id="nsTool" style={{color:"white", width: "50px"}}> Namespace </span></p>
This originally worked fine when the code was just using Javascript/React but when I converted the file to a .tsx (Typescript), I got the following error:当代码仅使用 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>'.
Does this mean that I can't use href in the span element?这是否意味着我不能在 span 元素中使用 href ? How can I resolve this?
我该如何解决这个问题?
HTML span
elements only allow global attributes . HTML
span
元素只允许全局属性。 So the href
attribute won't have an effect on span
and therefore you get a Typescript error.因此
href
属性不会对span
产生影响,因此您会收到 Typescript 错误。
Note: The href
attribute is usually only found on an anchor element
注意:
href
属性通常只在anchor element
上找到
<span>
tag does not have href
attribute. <span>
标签没有href
属性。 You can append span tag with anchor tag like this way:您可以像这样附加带有锚标记的 span 标记:
<span>
:
<a href="#">
Namespace
</a>
</span>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.