Html <a> 链接无法通过单击工作,只能通过“在新选项卡中打开链接”命令工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15050095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
<a> link not working by clicking, only work by "Open link in new tab" command
提问by Jeyhun Rahimov
I meet this interesting situation:
我遇到了这个有趣的情况:
<ul>
@foreach (var item in Model)
{
<li>
<a href="@Url.Action("Details", "Product", new { id = item.Id })" >@item.Name</a>
</li>
}
</ul>
When I click a link, nothing happen, product Details page does not open. But I do "Open link in new tab", then it opens. What can be it's reason?
当我点击一个链接时,什么也没有发生,产品详情页面也打不开。但我做了“在新标签中打开链接”,然后它打开了。可能是什么原因?
回答by Darin Dimitrov
You have some javascript code which is preventing the default action of the anchor tag to be executed. You could inspect the Network
tab in FireBug or Chrome DevTools to see if some AJAX request is being made when you click on the link. You could try excluding javascript files until you find the one that is doing this.
您有一些 javascript 代码阻止执行锚标记的默认操作。您可以检查Network
FireBug 或 Chrome DevTools 中的选项卡,以查看当您单击链接时是否正在发出某些 AJAX 请求。您可以尝试排除 javascript 文件,直到找到正在执行此操作的文件。
回答by Maq
Maybe you prevented the redirection event of a tag with JavaScript.
也许您使用 JavaScript 阻止了标签的重定向事件。
For example:
例如:
$(document).on('click', 'a.thatTag', function (e) {
// ...
e.preventDefault();
});
.preventDefault()
prevents redirection.
.preventDefault()
防止重定向。
回答by Paulo
Correctly given by Darren, it is definitely some javascript code that is sending an AJAX request (post or get) that is preventing the link from functioning as you would expect.
Darren 正确给出,肯定是一些 javascript 代码正在发送 AJAX 请求(发布或获取),阻止链接按预期运行。
However, if you've used an external resource, it might be difficult to correct the error in the min
files.
但是,如果您使用了外部资源,则可能难以更正min
文件中的错误。
I suggest use a jquery onclick event inline to circumvent the AJAX call
我建议使用内联的 jquery onclick 事件来规避 AJAX 调用
<a href="http://foo.bar/" onclick="window.open('https://www.foo.bar/culrsteam')">Foo</a>
You could additionally use window.open('https://www.foo.bar/culrsteam', '_blank')
like with target='_blank'
您还可以使用window.open('https://www.foo.bar/culrsteam', '_blank')
liketarget='_blank'
回答by djamahl
This may happen if the address does not have the transfer protocol within the href attribute. An example would be linking localhost as <a href="localhost"></div>
instead of <a href="http://localhost"></div>
. You will see that Google Chrome will give a cancelled status if done so, and in Safari a dialog will appear asking which application the link should be opened in, as the transfer protocol is unknown. I encountered this problem when setting the link to $_SERVER['HTTP_HOST']
in PHP.
如果地址在 href 属性中没有传输协议,则可能会发生这种情况。一个例子是将 localhost 作为<a href="localhost"></div>
而不是<a href="http://localhost"></div>
. 如果这样做,您将看到谷歌浏览器将显示取消状态,并且在 Safari 中会出现一个对话框,询问应在哪个应用程序中打开链接,因为传输协议未知。我$_SERVER['HTTP_HOST']
在PHP中设置链接时遇到了这个问题。
Key point being, make sure you leave the transfer protocol in front of the link be it http://
, https://
, ftp://
or whatever else.
关键点的存在,一定要留在链接前面的传输协议吧http://
,https://
,ftp://
或任何其他。
回答by Sujiraj R
Try this code...
试试这个代码...
<a href="http://google.com/" onclick="location.replace('http://google.com/'),'_top'">Google</a>
回答by Konstantin Zlatkov
I had e.preventDefault()
set on the parent element.
我已经e.preventDefault()
在父元素上设置了。
回答by Bangkokian
I know this is an old question, but having just had a similar issue, it sounds like the issue may be that your request is being blocked by the browser. This can happen if you're currently in a secure (https) environment trying to link to an insecure (http) resource.
我知道这是一个老问题,但刚刚遇到了类似的问题,听起来问题可能是您的请求被浏览器阻止了。如果您当前处于安全 (https) 环境中尝试链接到不安全 (http) 资源,则可能会发生这种情况。
It's not clear what your href is, but make sure the href is https (or //) if your current environment is secure.
不清楚您的 href 是什么,但如果您当前的环境是安全的,请确保 href 是 https(或 //)。