问题描述
我正在尝试使用 jQuery 触发超链接上的点击事件,如下所示.超链接没有任何 id 但它有 css 类:
I'm trying to trigger click event on hyperlink with jQuery like the way below. Hyperlink does not have any id but it does have css class:
$(document).ready(function () {
$('.cssbuttongo').trigger('click');
});
上面的功能不起作用.这是超链接:
The function above is not working. This is the hyperlink:
<a href="hyperlinkurl" class="cssbuttongo">hyperlink anchor</a>
推荐答案
我没有事实证据来证明这一点,但我已经遇到了这个问题.似乎在 <a>
标签上触发 click() 事件似乎与您期望的输入按钮的行为方式不同.
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.
我采用的解决方法是在窗口上设置 location.href 属性,这会导致浏览器像这样加载请求资源:
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
});
});
我会做一个 js 小提琴,但问题的性质与 jsfiddle 如何使用 iframe 来呈现代码混合在一起,这让我无法做到.
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元素上的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!