如何在href元素上触发点击事件 [英] How to trigger click event on href element

查看:231
本文介绍了如何在href元素上触发点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试通过jQuery触发超链接上的点击事件,如下所示.超链接没有任何ID,但确实具有cssclass

 $(document).ready(function () {  $('.cssbuttongo').trigger('click'); }); 

上面的功能不起作用.这是超链接

<a href="hyperlinkurl" class="cssbuttongo">hyperlink anchor</a>

解决方案

我没有事实证据来证明这一点,但是我已经遇到了这个问题.似乎在<a>标记上触发click()事件的行为似乎与输入按钮所期望的行为不同.

我采用的解决方法是在窗口上设置location.href属性,这将导致浏览器像这样加载请求资源:

$(document).ready(function()
{
      var href = $('.cssbuttongo').attr('href');
      window.location.href = href; //causes the browser to refresh and load the requested url
   });
});

我会做一个js小提琴,但是问题的本质与jsfiddle如何使用iframe渲染代码混杂在一起,

I am trying to trigger click event on hyperlink with jQuery like the way below. Hyperlink does not have any id but it does have cssclass

 $(document).ready(function () {  $('.cssbuttongo').trigger('click'); }); 

The function above is not working. This is the hyperlink

<a href="hyperlinkurl" class="cssbuttongo">hyperlink anchor</a>

解决方案

I do not have factual evidence to prove this but I already ran into this issue. It seems that triggering a click() event on an <a> tag doesn't seem to behave the same way you would expect with say, a input button.

The workaround I employed was to set the location.href property on the window which causes the browser to load the request resource like so:

$(document).ready(function()
{
      var href = $('.cssbuttongo').attr('href');
      window.location.href = href; //causes the browser to refresh and load the requested url
   });
});

Edit:

I would make a js fiddle but the nature of the question intermixed with how jsfiddle uses an iframe to render code makes that a no go.

这篇关于如何在href元素上触发点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆