我可以调用jquery click()跟随< a>链接如果我没有绑定一个事件处理程序与它绑定或点击已经? [英] Can I call jquery click() to follow an <a> link if I haven't bound an event handler to it with bind or click already?

查看:218
本文介绍了我可以调用jquery click()跟随< a>链接如果我没有绑定一个事件处理程序与它绑定或点击已经?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript中有一个计时器,需要模拟点击一个链接去另一个页面一旦时间流逝。为此,我使用jquery的click()函数。我已经使用$()。trigger()和window.location,我可以使它的工作原理与所有三个。

I have a timer in my javascript which needs to emulate clicking a link to go to another page once the time elapses. To do this I'm using jquery's click() function. I have used $().trigger() and window.location also, and I can make it work as intended with all three.

我观察到一些奇怪的行为与我点击(),我试图了解发生了什么和为什么。

I've observed some weird behavior with click() and I'm trying to understand what happens and why.

我使用Firefox的一切,我在这个问题描述,但我也感兴趣

I'm using Firefox for everything I describe in this question, but I am also interested in what other browsers will do with this.

如果我没有使用 $('a')。bind('click',fn) $('a')。click(fn)设置一个事件处理程序,然后调用$('a'什么都不做。

If I have not used $('a').bind('click',fn) or $('a').click(fn) to set an event handler, then calling $('a').click() seems to do nothing at all. It does not call the browser's default handler for this event, as the browser does not load the new page.

但是,如果我首先设置了一个事件处理程序,那么它会工作,因此它不会为浏览器调用此事件的默认处理程序。正如预期,即使事件处理程序不执行任何操作。

However, if I set an event handler first, then it works as expected, even if the event handler does nothing.

$('a').click(function(){return true;}).click();

这样会加载新页面,就像我自己点击了一个页面一样。

This loads the new page as if I had clicked the a myself.

所以我的问题是双重的:这是奇怪的行为,因为我在某处做错了什么?如果我没有创建自己的处理程序,为什么调用click()对默认行为没有作用?

So my question is twofold: Is this weird behavior because I'm doing something wrong somewhere? and Why does calling click() do nothing with the default behavior if I haven't created a handler of my own?

编辑:

霍夫曼决定当他试图复制我的结果,我上面描述的结果实际上不会发生。我不知道是什么原因导致我昨天观察到的事件,但我今天肯定不是我在问题中描述的。

As Hoffman determined when he tried to duplicate my results, the outcome I described above doesn't actually happen. I'm not sure what caused the events I observed yesterday, but I'm certain today that it was not what I described in the question.

所以答案是:你不能在浏览器中假的点击,所有的jquery都调用你的事件处理程序。

So the answer is that you can't "fake" clicks in the browser and that all jquery does is call your event handler. You can still use window.location to change page, and that works fine for me.

推荐答案

有趣的是,这可能是一个特征请求(即bug)jQuery。如果将jQuery事件绑定到元素,jQuery点击事件只会触发元素上的点击操作(在DOM上调用onClick事件)。您应该转到jQuery邮件列表( http://forum.jquery.com/ )并报告此问题。这可能是想要的行为,但我不这么认为。

Interesting, this is probably a "feature request" (ie bug) for jQuery. The jQuery click event only triggers the click action (called onClick event on the DOM) on the element if you bind a jQuery event to the element. You should go to jQuery mailing lists ( http://forum.jquery.com/ ) and report this. This might be the wanted behavior, but I don't think so.

编辑:

测试和你说的是错误的,即使你绑定一个函数到'a'标签,它仍然不会带你到href属性指定的网站。尝试以下代码:

I did some testing and what you said is wrong, even if you bind a function to an 'a' tag it still doesn't take you to the website specified by the href attribute. Try the following code:

<html>
<head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
 <script>
  $(document).ready(function() {
   /* Try to dis-comment this:
   $('#a').click(function () {
    alert('jQuery.click()');
    return true;
   });
   */
  });
  function button_onClick() {
   $('#a').click();
  }
  function a_onClick() {
   alert('a_onClick');
  }
 </script>

</head>
<body>
 <input type="button" onclick="button_onClick()">
 <br>
 <a id='a' href='http://www.google.com' onClick="a_onClick()"> aaa </a>

</body>
</html> 

除非您直接点击链接(有或没有注释的代码)。还要注意,即使您将点击事件绑定到链接,它仍然不会变为紫色,一旦你点击按钮。

It never goes to google.com unless you directly click on the link (with or without the commented code). Also notice that even if you bind the click event to the link it still doesn't go purple once you click the button. It only goes purple if you click the link directly.

我做了一些研究,似乎.click是不是使用'a'标签,因为浏览器不支持使用javascript的假点击。我的意思是,你不能点击带有javascript的元素。使用a标签,您可以触发其onClick事件,但链接不会更改颜色(到访问的链接颜色,在大多数浏览器中默认为紫色)。因此,将$()。click事件与a标记结合使用是没有意义的,因为进入href属性的行为不是onClick事件的一部分,而是在浏览器中硬编码。

I did some research and it seems that the .click is not suppose to work with 'a' tags because the browser does not suport "fake clicking" with javascript. I mean, you can't "click" an element with javascript. With 'a' tags you can trigger its onClick event but the link won't change colors (to the visited link color, the default is purple in most browsers). So it wouldn't make sense to make the $().click event work with 'a' tags since the act of going to the href attribute is not a part of the onClick event, but hardcoded in the browser.

这篇关于我可以调用jquery click()跟随&lt; a&gt;链接如果我没有绑定一个事件处理程序与它绑定或点击已经?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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