簡體   English   中英

jQuery 事件直到第二次點擊才觸發?

[英]jQuery event not firing until second click?

我有以下 jQuery 代碼。 我第一次點擊鏈接時,什么也沒有發生; 但是,在第二次單擊該鏈接時,會發生預期的操作。

$("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
      }
   });
});

這是我的 HTML:

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

有誰知道我如何解決這個問題? 提前感謝您的任何幫助。

嘗試

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

或者您可以嘗試..,但我認為這不會解決它。

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

並確保它在 DOM 中准備就緒。


您可以嘗試阻止默認操作,也許這會干擾您的第一次點擊。

 $("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 } }); });

首先,使用 $(document).ready({}) 語句嵌入您的代碼。 它確保只有在瀏覽器解析了所有 html 語法后,您的 dom 才會受到代碼的影響。

嘗試

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

查看您的 HTML 代碼是否有問題。 然后您可以添加其他語句以調試您的 js 代碼

來自: http://flowplayer.org/tools/overlay/index.html

// select one or more elements to be overlay triggers

將覆蓋設置為 HTML 的點擊屬性可能是多余的。 基於上述,我會假設覆蓋已經在點擊時被激活。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM