简体   繁体   English

如何访问a内的href<link> 在 react-router-dom 中标记?

[英]How to access a href inside a <Link> tag in react-router-dom?

I have a href tag inside a Link tag from react-router-dom.我在 react-router-dom 的 Link 标签中有一个 href 标签。 The Link works fine in my React.js app.该链接在我的 React.js 应用程序中运行良好。 I am trying to access the href tag which sits inside the Link tag, but when I do click I am redirected to the route path specified in my app Link instead of the href URL.我正在尝试访问位于 Link 标记内的 href 标记,但是当我单击时,我被重定向到我的应用程序 Link 中指定的路由路径,而不是 href URL。

<div class="col-md-8">
    <Link id="homeLink" to="/graduate-posts">
        <img class="img-fluid" src="https://i.imgur.com/vekPedd.png" alt="Card image cap" />
        <div class="card-img-overlay">
            <div class="homeLinkTextContainer">
                <div id="homeLinkTitleContainer">
                    <p id="homeLinkTitle">Graduates</p>
                </div>
                <div class="homeLinkText" id="homeLinkTextG">
                    <p>Coming Soon! Want To <strong><a href="click.com" id="wfu"> Write For Us? </a></strong></p>
                </div>
            </div>
        </div>
    </Link>
</div>

I am trying to access the href tag that has the id="wfu".我正在尝试访问具有 id="wfu" 的 href 标记。

When I hover over it, I see the URL on the browser through Chrome but when I click it, instead of going to the href URL which is what I want, it goes to react-router path. When I hover over it, I see the URL on the browser through Chrome but when I click it, instead of going to the href URL which is what I want, it goes to react-router path.

Screenshot on browser浏览器截图

I tried the z-index but that did not work.我尝试了 z-index,但没有奏效。 Any help would be highly appreciated.任何帮助将不胜感激。

Try to stop the propagation so the click event doesn't bubble up to the Link :尝试停止传播,以便点击事件不会冒泡到Link

import React from 'react';

function ExampleComponent() {
  const stopEvent = (event) => event.stopPropagation();
  wfuElement= document.getElementById("wfu");
  wfuElement.addEventListener("click", stopEvent, false);

  return (
    // your jsx above
  )
}

export default ExampleComponent;

Alternative Solution替代解决方案

Add a click handler to the a element:a元素添加点击处理程序:

import React from 'react';

function ExampleComponent() {
  const stopEvent = (event) => event.stopPropagation();

  return (
    // removed everything but anchor for brevity
    <a href="click.com" id="wfu" onClick={stopEvent}> Write For Us? </a>
  )
}

export default ExampleComponent;

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

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