Icons in jQuery UI dialog title

后端 未结 5 917
一整个雨季
一整个雨季 2020-12-28 15:47

I\'m using the code below to create a jQuery dialog box. By default there is a close icon on title bar, but I want to add some of the other icons in title bar as well for di

相关标签:
5条回答
  • 2020-12-28 16:29

    You can do that by adding some HTML code to title bar when dialog opened.

    $("document").ready(function () {
            $("#dialog").dialog({
                title: "Dialog Title",
                height: 400,
                width: 500,
                open: function(event, ui){
                    $(this).parent().find('.ui-dialog-titlebar').append('Some HTML');
                }
            });
        });
    
    0 讨论(0)
  • 2020-12-28 16:33

    Here is how you solve the jQuery UI 1.10.0 dialog title issue globally instead of one object at a time:

    jQuery.widget('ui.dialog', jQuery.extend({}, jQuery.ui.dialog.prototype, {
        _title: function(titleBar) {
            titleBar.html(this.options.title || ' ');
        }
    }));
    

    Now use the dialog widget as usual and your titles won't get escaped anymore.

    0 讨论(0)
  • 2020-12-28 16:34

    I did it this way:

    <script type="text/javascript" language="javascript">
        function MsgBox() {
            var arg = arguments;
            /*
            [arg]
            0 = message
            1 = title
            2 = width
            3 = height
            4 = command to evaluete if Ok is clicked (optional)
            */
            $("body").append("<div id=\"dlgMsg\" title=\"" + arg[1] + "\">" + arg[0] + "</div>");
            $("#dlgMsg").dialog({
                autoOpen: false,
                modal: true,
                bgiframe: true,
                width: arg[2],
                height: arg[3],
                close: function (event, ui) { $(this).dialog("destroy").remove(); },
                buttons:{ 
                        'OK': function () { if (arg[4]) eval(arg[4]); $(this).dialog("close"); }
                        }
            });
    
            $("#dlgMsg").dialog('open');
            return false;
        }
    </script>
    

    Usage: MsgBox("Hello, I'm a MessageBox!", "Title Here", 400, 200);

    Or

    MsgBox("Hello, I'm a MessageBox!", "Title Here", 400, 200, "alert('hey, you clicked Ok!')");

    You may improve it with ui-icons as well...

    0 讨论(0)
  • 2020-12-28 16:49

    You can define HTML in the title option when creating the dialog box. Therefore, using the existing jQuery UI icon sprites, we can use this Javascript:


    For jQuery UI 1.10.0

    You need to override the undocumented _title function, according to Bug #6016 to ensure that the title attribute is not escaped.

    var dialog = $("#dialog").dialog();
    
    dialog.data( "uiDialog" )._title = function(title) {
        title.html( this.options.title );
    };
    
    dialog.dialog('option', 'title', '<span class="ui-icon ui-icon-home"></span> Example Dialog');
    

    For older versions

    $("#dialog").dialog({
        title: '<span class="ui-icon ui-icon-home"></span> Example Dialog'
    });
    

    And this CSS

    .ui-dialog .ui-dialog-title .ui-icon {
        float: left;
        margin-right: 4px;
    }
    

    To add an icon to the title of the dialog. You can see the complete set of jQuery UI icons here: http://jqueryui.com/themeroller/

    To see this working, live, see: http://jsfiddle.net/XkSu9/ (jQuery UI 1.10+) or http://www.jsfiddle.net/yijiang/UYMJH/ (old version)

    0 讨论(0)
  • 2020-12-28 16:51

    A much simpler method. In the stylesheet:

    .ui-dialog .ui-dialog-title {
      background-image: url('/icons/info.png');
      background-repeat: no-repeat;
      padding-left: 20px;
    }
    

    This is for a 16x16 image.

    0 讨论(0)
提交回复
热议问题