为什么我无法在jQuery中单击click()链接? [英] Why can't I click() links in jQuery?
问题描述
我在jQuery中遇到了一个好奇:如果在链接上调用.click()
,则会调用click
事件处理程序,但实际上并没有遵循该链接(就像在浏览器中单击了链接一样):>
I came across a curiosity in jQuery: if I call .click()
on a link the click
event handlers are called, but the link isn't actually followed (as if it were clicked in the browser):
<a id="link" href="http://www.google.com>Link</a>
$("#link").click() // won't take me to Google
但是在普通的Javascript中,一切都按预期运行:
But in plain Javascript, everything behaves as expected:
document.getElementById("link").click() // *will* take me to Google
这显然是故意行为-但是我我正在努力弄清为什么click
如此实现-链接有特殊例外?
This is apparently intentional behaviour - but I'm struggling to work out why click
was implemented like this - with a special exception for links?
在这里拨弄: http://jsfiddle.net/9a6sp/
澄清一下:我不是在问如何在JS中单击链接,而是为什么在jQuery中默认的行为是有效地使您在调用.click()
时未点击链接 >
To clarify: I'm not asking how to click link in JS, but rather, why the default behaviour in jQuery is effectively that links aren't clicked when you call .click()
推荐答案
domelement.click()
不支持跨浏览器进行重定向.如果您需要重定向到链接中的位置,则可以使用:
domelement.click()
isn't supported cross browser for redirecting. If you need to redirect to the location in a link you can use:
window.location = $('#link').prop('href');
这篇关于为什么我无法在jQuery中单击click()链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!