2010-09-23 121 views
0
$(top.document).ready(function() { 

    $("*").click(processAction); 



function processAction(e){ 
    var clicked = e.target; 
    e.stopPropagation(); 
    alert(clicked.tagName); 
    e.stopPropagation(); 

    switch(clicked.tagName) 
    { 
     case "A": 
      newDialog("You've clicked on a link !", "Please Choose An Action <br> 1<br>3");   
      return false; // prevent the default action, e.g., following a link 
     break; 

     case "INPUT": 
      newDialog("You've clicked on a form !", "Please Choose An Action <br> 1<br>3");   
      return false; 
     break; 

     case "IMG": 
      newDialog("You've clicked on a picture !", "Please Choose An Action <br> 1<br>3");   
      return false; 
     break; 

     case "BUTTON": 
      newDialog("You've clicked on a button !", "Please Choose An Action <br> 1<br>3");   
      return false; 
     break; 

     default: 
      newDialog("You've clicked on a text !", "Please Choose An Action <br> 1<br>3");   
      return false; 
    } 
}; 

function newDialog(mytitle, mycontent){ 
    var $dialog = $('<div></div>') 
     .html(mycontent) 
     .dialog({ 
      autoOpen: false, 
      open: function() {$("*").unbind('click', processAction);}, 
      close: function() {$("*").bind('click', processAction);}, 
      title: mytitle, 
      buttons: { "Cancel": function() { $(this).dialog("close"); } 
        } 
     }); 
    $dialog.dialog('open'); 

} 

我試着設置綁定和取消綁定點擊打開和關閉事件,在processAction()函數,但是,它似乎並沒有被工作按計劃進行。jQuery的:在用戶界面對話框禁用點擊功能

回答

0

使用

e.preventDefault(); 

API

http://api.jquery.com/event.preventDefault/

例如

function newDialog(mytitle, mycontent){ 
    var $dialog = $('<div id="something"></div>') 
     .html(mycontent) 
     .dialog({ 
      autoOpen: false, 
      open: function() {$("*").unbind('click', processAction);}, 
      close: function() {$("*").bind('click', processAction);}, 
      title: mytitle, 
      buttons: { "Cancel": function() { $(this).dialog("close"); } 
        } 
     }); 
    $dialog.dialog('open'); 
} 

$('#something a').live('click', function(e){ e.preventDefault(); }); # fixed spelling 
1

永遠不要做類似$("*").click(processAction);。這是非常低效的,加上它基本上陷阱每一個點擊事件,這就是爲什麼它也會導致你的對話框出現問題。

更好的方法是在某個容器中包含您感興趣的所有內容,並選擇該容器內的所有元素。這仍然不是一件好事,這引出了一個問題:你想做什麼?也許有更好的方法來做任何你現在想做的事情。

相關問題