简体   繁体   English

jQuery 事件直到第二次点击才触发?

[英]jQuery event not firing until second click?

I have the following jQuery code.我有以下 jQuery 代码。 The first time I click a link, nothing happens;我第一次点击链接时,什么也没有发生; however, upon clicking the link the second time, the expected action occurs.但是,在第二次单击该链接时,会发生预期的操作。

$("a[rel]").bind("click", function(){
   $(".close_button_change_password").attr('id','change_password_'+$(this).attr('id'));
   $(this).overlay({
      // disable this for modal dialog-type of overlays
      closeOnClick: true,
      mask: {
         color: '#fff',
         loadSpeed: 200,
         opacity: 0.8
      }
   });
});

Here's my HTML:这是我的 HTML:

<a href="#" rel="#change_password" id="1">change password</a>

Does anyone know how I might go about fixing this problem?有谁知道我如何解决这个问题? Thanks in advance for any assistance.提前感谢您的任何帮助。

Try尝试

$("a[rel]").live("click", function(){ ....

or you could try.., but I dont think this would solve it.或者您可以尝试..,但我认为这不会解决它。

$("a[rel]").live("change", function(){ ....   

And make sure its in DOM ready.并确保它在 DOM 中准备就绪。


You could try and prevent the default action, maybe this is interfering with your first click. 您可以尝试阻止默认操作,也许这会干扰您的第一次点击。

 $("a[rel]").bind("click", function(event){ event.preventDefault(); // <---------^^ $(".close_button_change_password").attr('id','change_password_'+$(this).attr('id')); $(this).overlay({ // disable this for modal dialog-type of overlays closeOnClick: true, mask: { color: '#fff', loadSpeed: 200, opacity: 0.8 } }); });

First of all, embed your code with $(document).ready({}) statement.首先,使用 $(document).ready({}) 语句嵌入您的代码。 It makes sure you'r dom will be affected with code only after all html syntax is parsed by browser.它确保只有在浏览器解析了所有 html 语法后,您的 dom 才会受到代码的影响。

Try尝试

$("a[rel]").click(function(){alert($(this).attr("id"));});

to see if there is something wrong with your HTML code.查看您的 HTML 代码是否有问题。 then you can add other statement in order to debug your js code然后您可以添加其他语句以调试您的 js 代码

From: http://flowplayer.org/tools/overlay/index.html来自: http://flowplayer.org/tools/overlay/index.html

// select one or more elements to be overlay triggers

Setting the overlay to the click attribute of HTML might be redundant.将覆盖设置为 HTML 的点击属性可能是多余的。 Based on the above, I would assume that the overlay is already activated on click.基于上述,我会假设覆盖已经在点击时被激活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM