<div id="container">
<input type="text" id="textBox" />
<a id="anchorTest" href="javascript: alert('inside anchorTest')">Click me</a>
</div>
单击Enter键时,我试图将默认按钮设为锚点:
$(document).ready(function () {
$('#anchorTest').click(function () {
alert('clicked');
});
$('#container').keyup(function (e) {
if (e.keyCode == 13) {
$('#anchorTest').triggerHandler('click');
}
});
});
但是从不调用anchor href javascript吗?
如何使用JQuery单击锚点,以便也调用href?
我使用下面的代码,它可以工作,但是有什么更好的方法,因为它提交了表单,而我想拥有一个纯JavaScript代码:
window.location = $('#anchorTest').attr('href');
谢谢
解决方法:
jQuery以“特殊”方式处理锚标记上的click事件.有关更多信息,请参见此问题-Can I call jquery click() to follow an <a> link if I haven’t bound an event handler to it with bind or click already?
根据上述问题-
I did some research and it seems that the .click is not suppose to
work with ‘a’ tags because the browser does not suport “fake clicking”
with javascript. I mean, you can’t “click” an element with javascript.
With ‘a’ tags you can trigger its onClick event but the link won’t
change colors (to the visited link color, the default is purple in
most browsers). So it wouldn’t make sense to make the $().click event
work with ‘a’ tags since the act of going to the href attribute is not
a part of the onClick event, but hardcoded in the browser.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。