简体   繁体   English

jQuery元素点击

[英]jQuery element click

I have an element, an anchor, how can I "click it" with jQuery, I mean like a normal user click, because I'm receiving a click on an element..but also I need to fire more events... 我有一个元素,一个锚点,我该如何使用jQuery“单击它”,我的意思是像普通用户一样单击,因为我收到了对元素的单击。但是我还需要触发更多事件...

Ok here is my example 好的,这是我的例子

<a class="som" href="http://domain.com/ssl-signup.php" target="_blank">Test Link</a>
$(document).ready(function(){
   $('.som').click();
});

But nothing happens! 但是什么也没发生!

Best Regards. 最好的祝福。

$("myelement").click();

or 要么

$("myelement").trigger("click");

Second is useful if you need to decide event type in runtime. 如果需要在运行时确定事件类型,第二个很有用。

Firing the click action for an anchor will not redirect the user to the anchor's URL as you would expect with a normal click . 触发锚点的click操作不会像常规click那样将用户重定向到锚点的URL。

Instead, you'll need to do this: 相反,您需要执行以下操作:

$('#anchorId').click(function(){
  window.location.href = $(this).attr('href');
});

EDIT -- CORRECT ANSWER BELOW 编辑-下面的正确答案

I misunderstood the question. 我误解了这个问题。 To accomplish what you want, just make this call to simulate the click : 要完成您想要的操作,只需进行以下调用即可模拟click

window.location.href = $('#anchorId').attr('href');
$(element).click();

Should work. 应该管用。

See: http://docs.jquery.com/Events/click 参见: http : //docs.jquery.com/Events/click

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

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