Jquery如何触发href元素上的click事件 [英] Jquery how to trigger click event on href element
问题描述
我尝试使用jquery触发超链接上的单击事件,如下所示。超链接没有任何id,但它有cssclass
$(document).ready(function(){$('。 cssbuttongo')。trigger('click');});
上述功能无法使用。这是超链接
< a href =hyperlinkurlclass =cssbuttongo> hyperlink anchor< / a>感谢您的回答。 解决方案我没有事实证据证明这一点,但我已经遇到了这个问题。看起来在< a>
标签上触发click()事件似乎没有与您所说的输入按钮一样的行为。 p>
我采用的解决方法是在窗口上设置location.href属性,使浏览器加载请求资源,如下所示:
$(document).ready(function()
{
var href = $('。cssbuttongo')。attr('href') ;
window.location.href = href; //导致浏览器刷新并加载请求的url
});
});
编辑:
js fiddle但是问题的性质混合了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>
Thanks for the answers.
解决方案 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.
这篇关于Jquery如何触发href元素上的click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!