如何使用jQuery触发器锚的默认点击事件? [英] How to use jquery trigger anchor's default click event?

查看:86
本文介绍了如何使用jQuery触发器锚的默认点击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在页面中有一个锚点,如:<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屋!

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