我可以叫jQuery的click()跟随< a>吗?如果尚未通过绑定或单击将事件处理程序绑定到该链接,该链接是否已链接? [英] Can I call jQuery's click() to follow an <a> link if I haven't bound an event handler to it with bind or click already?

查看:260
本文介绍了我可以叫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.

我已经观察到click()的某些奇怪行为,并且我试图了解会发生什么以及为什么.

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').click()似乎什么都没有.它不会为该事件调用浏览器的默认处理程序,因为浏览器不会加载新页面.

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所做的全部就是调用您的事件处理程序.您仍然可以使用window.location来更改页面,对我来说效果很好.

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.

推荐答案

有趣的是,这可能是jQuery的功能请求"(即错误).如果将jQuery事件绑定到元素,则jQuery click事件仅触发元素上的click动作(在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> 

除非您直接单击链接(带有或不带有注释代码),否则它永远不会进入google.com.还要注意,即使将click事件绑定到链接,单击按钮也不会变成紫色.如果您直接单击链接,它只会变成紫色.

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天全站免登陆