jQuery UI dialog button focus

前端 未结 13 2134
时光取名叫无心
时光取名叫无心 2020-12-13 12:08

When a jQuery UI dialog opens, it selects one of the buttons and highlights it or sets focus to it etc... How can I stop this behaviour so that none of the buttons are highl

相关标签:
13条回答
  • 2020-12-13 12:36

    Base on ED and Lev's answers, I got this good solution:

        jQuery("#testDialog").dialog({
            height: 280,
            width: 400,
    
            open: function () {             
                jQuery(this).closest( ".ui-dialog" ).find(":button").blur();
            }
        });
    
    0 讨论(0)
  • 2020-12-13 12:36

    I stumbled upon a little hack to fix this that others may find useful. A solution like this seems to work for me:

    $( "#button" ).click(function() {                                   
        // this is a hack to prevent the focus from remaining after a click
        $(this).toggle(this.checked);                                       
    });
    

    It simply programmatically sets it checked again, which seems to clear up the focus issue.

    0 讨论(0)
  • 2020-12-13 12:39

    thanks for answers, but it seems to me that the best solution (at least for me, minimal code and no unnecessary use of methods on the DOM) is to define your dialog buttons in an array of object :

    buttons: [{
                id  :   "button1",
                text    :   "Button1 text",
                title   :   "tooltip1text1",
                tabIndex:   -1,
                click   :   clickCallback 
            },{
                id      :   "button2",
                text    :   "Button2 text", 
                title   :   "tooltip1text2",
                tabIndex:   -1,
                click   :   function(){$(this).dialog("close")}
            }]
    

    The important part to prevent your buttons to get focus is : tabIntex:-1

    It is also a convenient and readable way to give id to your buttons.

    0 讨论(0)
  • 2020-12-13 12:39
    buttons: [
        {
            tabIndex: -1,
            text: 'Yes',
            click: function(){
                DeleteStuff();
                $(this).dialog('close');
            }
        },
        {
            text: 'No',
            click: function(){
                // Don't delete
                $(this).dialog('close');
            }
        }
    ]
    

    This is the only way I got it working. tabIndex: -1 is the solution.

    Oh, and I wanted to focus on the second button, so my "Delete Item?" confirmation would by default not delete the item.

    0 讨论(0)
  • 2020-12-13 12:41

    I could do this in this way. jquery-ui-1.12.0.custom/jquery-ui.css -> onuline:none add on 896 line ouline:none

    .ui-widget button {
            font-family: Arial,Helvetica,sans-serif;
            font-size: 1em;
            outline:none;
        }
    
    0 讨论(0)
  • 2020-12-13 12:43

    I had this same problem... Trying to set the focus to another element didn't seem to do the trick for me, but blurring focus from the selected element (in the "open" callback) did:

        $('#dialog').dialog
        ({
        open: function ()
            {
            $('#element-that-gets-selected').blur();
            }
        });
    

    I suppose a way to prevent focus without specifying a specific name would be to use a selector with first, like this:

        $('#dialog').dialog
        ({
        open: function ()
            {
            $(this).find('select, input, textarea').first().blur();
            }
        });
    
    0 讨论(0)
提交回复
热议问题