如何使用jQuery触发器锚的默认点击事件? [英] How to use jquery trigger anchor's default click event?
问题描述
假设我在页面中有一个锚点,如:<a href="#header" id="anchor">turn to header</a>
,然后在页面<input type="button" value="triger anchor">
中也设置了一个按钮.我想做的是,当我单击该按钮时,它将触发锚点的默认单击事件,因为页面转到页眉".所以我写了$("input").click(function(){$("#anchor").trigger("click");});
,但似乎不起作用.那我该怎么实现呢?谢谢
Suppose I have an anchor in my page like :<a href="#header" id="anchor">turn to header</a>
then I also set a button in my page <input type="button" value="triger anchor">
.What I want to do is that ,when I click the button ,it will triger the anchor's default click event ,as the page turn to "header".So I write $("input").click(function(){$("#anchor").trigger("click");});
But it seems doesn't work .So how can I achieve that?Thank you
推荐答案
请参阅尝试设置window.location
属性,而不是尝试触发click
事件:
Instead of trying to trigger the click
event, try setting the window.location
property:
window.location = '#header';
或者也许:
$("input").click(function() {
window.location = $('#anchor').attr('href');
});
这篇关于如何使用jQuery触发器锚的默认点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!