简体   繁体   English

如何在导航到不同页面之前执行 onclick?

[英]How to execute onclick before navigating to a different page?

I am working in React and I have the following scenario.我在 React 中工作,我有以下场景。

  <a href={link} variant="primary" onClick={this.onClickDoSomething}>
                          here.
  </a>

Now, I want the onclick to execute before the link has been clicked.现在,我希望在单击链接之前执行 onclick。 However I do not see it happening.但是我没有看到它发生。

How to work around this issue ?如何解决这个问题? Thanks.谢谢。

Note the onclick() will fire before the page is redirected. 请注意,在重定向页面之前,将触发onclick() It appears that you may want to handle your navigation within your onclick function as opposed to having an href attribute. 似乎您可能希望在onclick函数中处理导航,而不是拥有href属性。 By doing it this way you can conditionally redirect (if need be) and/or wait for some async code to complete. 通过这种方式,您可以有条件地重定向(如果需要)和/或等待一些异步代码完成。

 onClick = url => { alert('Do something'); window.location.href = url; } 
 <a href='#' onclick="onClick('https://stackoverflow.com')">here.</a> 

React snippet: 反应代码段:

 class App extends React.Component { onClickDoSomething = link => ev => { alert(link); // Do any operations here window.location.href = link; } render() { const link = 'https://stackoverflow.com'; return <div> <a href='#' onClick={this.onClickDoSomething(link)}>here.</a> </div>; } } ReactDOM.render( <App/> , document.querySelector("#app")) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id='app'></div> 

your click handler should be like this 您的点击处理程序应该是这样的

onClickDoSomething=(link)=>{
//do something,then redirect to given link
window.location.href=link

}

and you should render link as 并且您应该将链接呈现为

<a  variant="primary" onClick={()=>{this.onClickDoSomething(link)}}>
                      here.
</a>

First will suggest use react router but if link is external then another way is keep href="#" and write onclickHandler function at the end onclickHandler just use window.location=[link] 首先将建议使用React Router,但如果链接位于外部,则另一种方法是keep href =“#”并在最后将onclickHandler函数写入onclickHandler,只需使用window.location = [link]

you want to visit 你想参观

Hope it helps 希望能帮助到你

All the given answers (ie using # for the href then adding window.location to your click handler) should work perfectly.所有给定的答案(即使用 # 作为 href 然后将 window.location 添加到您的点击处理程序)应该可以完美地工作。

However, in case you're looking for another solution, you can also try making the link a button instead of an anchor tag, and just styling it as a button.但是,如果您正在寻找其他解决方案,您也可以尝试将链接设置为按钮而不是锚标记,并将其样式化为按钮。 Below I've done so using Bootstrap, but you could do so with just plain CSS as well:下面我使用 Bootstrap 完成了此操作,但您也可以仅使用纯 CSS 执行此操作:

 handleClickExamQuestions = function(URL){ window.location=URL }
 <button class="btn btn-link" onClick = {()=>this.handleClick(URL)}>Click Me</button>

You can use the mouseenter event, which is triggered when you hover over the link. 您可以使用mouseenter事件,将鼠标悬停在链接上时会触发该事件。 Which means, it would be triggered before click event 这意味着它将在click事件之前触发

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

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