[英]Why can't I trigger a click event on a hyperlink through code
I am trying to implement a manual click event on a hyper-link using jquery but I am not getting how to do it.. 我正在尝试使用jquery在超链接上实现手动点击事件,但我没有得到如何做。
Here is my sample fiddle link [1]: http://jsfiddle.net/akki/XyVDd/1/ 这是我的样本小提琴链接[1]: http : //jsfiddle.net/akki/XyVDd/1/
<input type="text" class="example" id="opval" value="back">
<a id="sub_name" href="http://www.google.co.in">abc</a>
$(document).ready(function(){
if($('#opval').val() =='back') {
$('#sub_name').click();
//$('#qwe').find('a').trigger('click');
}
$('#sub_name').click(function(){
alert(hi)
});
});
A few minor problems here: 1. You don't have jQuery loaded in your fiddle. 这里有一些小问题:1.您的小提琴中没有加载jQuery。 2. You need to have
"hi"
in quotes in your alert()
. 2.您需要在
alert()
中的引号中alert()
"hi"
。
The main thing though is that you're triggering the click event before your click handler is attached. 不过,最主要的是,您在附加点击处理程序之前会触发click事件。 Just move the handler code to before the click trigger:
只需将处理程序代码移至click触发器之前:
$(document).ready(function(){
$('#sub_name').click(function(){
alert("hi");
});
if($('#opval').val() =='back') {
$('#sub_name').click();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.