jquery:在ui对话框上禁用单击功能

时间:2010-09-23 01:26:20

标签: jquery

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

}

我尝试设置打开和关闭事件来绑定和取消绑定click,processAction()函数,但是,它似乎没有按计划工作。

2 个答案:

答案 0 :(得分:1)

不要做$("*").click(processAction);之类的事情。这是非常低效的加上它基本上捕获每个点击事件,这也是它导致你的对话问题的原因。

更好的方法是在特定容器中包含您感兴趣的所有内容,并选择该容器内的所有元素。这仍然不是一件好事,这引出了一个问题:你想做什么?也许有更好的方法去做你目前正在做的事情。

答案 1 :(得分: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