简体   繁体   English

函数onclick可以打开新链接,而不能转到原始链接

[英]Function onclick makes open new link, no go to original link

I have a href tag with a link to another page. 我有一个href标签,并带有指向另一个页面的链接。

 <a name="mylink" class="mylink" href="path-to-new-page.html">Link to new page</a>

What I am trying to achieve here is that when the user clicks on the link, a onclick event is triggered. 我要在这里实现的是,当用户单击链接时,将触发onclick事件。 something like a return confirm. 诸如退货确认之类的东西。

onclick="return confirm('would you like to see our help center first?')"

If the user clicks "YES", then he is redirected to the help.html page. 如果用户单击“是”,那么他将被重定向到help.html页面。

If he clicks on "NO", he is sent to the link of the original href . 如果单击“否”,则会将其发送到原始href的链接。

Must I use jquery for this with a popup window instead of a alert box? 为此,我必须使用jquery而不是弹出窗口吗?

To answer your second question first, you do not have to, as you can get the result of the alert box inline in your Javascript. 您不必先回答第二个问题,因为您可以在Javascript中内嵌警报框的结果。

As for the onclick on the <a> tag, that sounds exactly like a button. 至于<a>标记的onclick,听起来完全像一个按钮。 Same syntax as a link except you would do something like this: 与链接的语法相同,除了您可以执行以下操作:

<button onclick='myFunction()'>Click Here</button>

Then your javascript would look like: 然后您的JavaScript看起来像:

function myFunction(){
    if(confirm("Would you like to see our help center first?")){
        // what happens if they select yes //
        document.href.location = "helpcenter.html";
    }else{
        // what happens if they select no //
        document.href.location = "linktarget.html";
    }
}

Where, of course, helpcenter.html is your Help Center, and linktarget.html is the original target URL of the link. 当然,其中helpcenter.html是您的帮助中心, linktarget.html是链接的原始目标URL。

confirm() just returns a boolean on whether they pressed okay ( true ) or cancel or the x at the top of the dialog ( false ), and document.href.location is exactly the same as saying <a href=' confirm()只是返回一个布尔值,表示是否按下okay( true )或cancel或对话框顶部的xfalse ), document.href.location与说<a href='完全相同

Disclaimer: I used personal URLs for this demo, they may vanish in the future, but their names should still be easy to understand. 免责声明:我在此演示中使用了个人URL,将来它们可能会消失,但其名称仍应易于理解。

Without jQuery 没有jQuery

 var links = document.getElementsByClassName('myLink'); for(var i=0, l=links.length; i<l; i++){ links[i].addEventListener('click', function(e){ var goToHelpCenter = confirm('Would you like to see our help center first?'); if(goToHelpCenter){ // Prevent the link from going to its default href e.preventDefault(); // Go to help center window.location.href = 'http://shrt.tf/help-center.html'; } // Otherwise, do nothing. }); } 
 <a class="myLink" href="http://shrt.tf/page.html">Click me</a> 

jQuery version jQuery版本

 $('.myLink').click(function(e){ var goToHelpCenter = confirm('Would you like to see our help center first?'); if(goToHelpCenter){ // Prevent the link from going to its default href e.preventDefault(); // Go to help center window.location.href = 'http://shrt.tf/help-center.html'; } // Otherwise, do nothing. }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a class="myLink" href="http://shrt.tf/page.html">Click me</a> 

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

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