简体   繁体   English

如何将 ID 属性添加到从 react-router 链接标签呈现的锚点标签?

[英]How to add an ID attribute to the anchor tag that is rendered from react-router link tag?

// modules/App.js <li><Link to="/about" activeStyle={{ color: 'red' }}>About</Link></li> <li><Link to="/repos" activeStyle={{ color: 'red' }}>Repos</Link></li>

when inspecting the DOM using Chrome Developer tools I find that the Link Component renders an tag.使用 Chrome 开发人员工具检查 DOM 时,我发现链接组件呈现了一个标签。 So I wan to know how to add an id attribute to the resulting a tag所以我想知道如何将 id 属性添加到结果标签

<a id="foo">

i have tried this but it didn't work :我试过这个,但没有用:

<li><Link id="foo" to="/about" activeStyle={{ color: 'red' }}>About</Link></li>

From the docs:从文档:

You can also pass props you'd like to be on the such as a title, id, className, etc.您还可以传递您想要的道具,例如标题、id、className 等。

http://knowbody.github.io/react-router-docs/api/Link.html http://knowbody.github.io/react-router-docs/api/Link.html

Which version of react-router are you using.您使用的是哪个版本的 react-router。 Make sure that the id is not rendered, by inspecting the element using a dev console.通过使用开发控制台检查元素,确保未呈现 id。

It can be directly done by adding and ID attribute to the tha link tag:可以直接通过在 tha 链接标签中添加和 ID 属性来完成:

<Link id="myId"></Link>

the rendered anchor tag will conatin the ID attribute that was given to link tag.呈现的锚标记将包含赋予链接标记的 ID 属性。

You can also pass props you'd like to be on the such as a title, id, className, etc.您还可以传递您想要的道具,例如标题、id、className 等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM