How to change the style of alert box?

后端 未结 13 1969
一生所求
一生所求 2020-11-22 01:49

I need to change the style of the \"OK\" Button in an alert box.

相关标签:
13条回答
  • 2020-11-22 02:31

    I use SweetAlert, It's Awesome, You will get lots of customization option as well as all callbacks

    swal("Here's a message!", "It's pretty, isn't it?");
    

    0 讨论(0)
  • 2020-11-22 02:32

    I use AlertifyJS to style my dialogues.

    alertify.alert('Ready!');
    alertify.YoutubeDialog || alertify.dialog('YoutubeDialog',function(){
        var iframe;
        return {
            // dialog constructor function, this will be called when the user calls alertify.YoutubeDialog(videoId)
            main:function(videoId){
                //set the videoId setting and return current instance for chaining.
                return this.set({ 
                    'videoId': videoId
                });
            },
            // we only want to override two options (padding and overflow).
            setup:function(){
                return {
                    options:{
                        //disable both padding and overflow control.
                        padding : !1,
                        overflow: !1,
                    }
                };
            },
            // This will be called once the DOM is ready and will never be invoked again.
            // Here we create the iframe to embed the video.
            build:function(){           
                // create the iframe element
                iframe = document.createElement('iframe');
                iframe.frameBorder = "no";
                iframe.width = "100%";
                iframe.height = "100%";
                // add it to the dialog
                this.elements.content.appendChild(iframe);
    
                //give the dialog initial height (half the screen height).
                this.elements.body.style.minHeight = screen.height * .5 + 'px';
            },
            // dialog custom settings
            settings:{
                videoId:undefined
            },
            // listen and respond to changes in dialog settings.
            settingUpdated:function(key, oldValue, newValue){
                switch(key){
                   case 'videoId':
                        iframe.src = "https://www.youtube.com/embed/" + newValue + "?enablejsapi=1";
                    break;   
                }
            },
            // listen to internal dialog events.
            hooks:{
                // triggered when the dialog is closed, this is seperate from user defined onclose
                onclose: function(){
                    iframe.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}','*');
                },
                // triggered when a dialog option gets update.
                // warning! this will not be triggered for settings updates.
                onupdate: function(option,oldValue, newValue){
                    switch(option){
                        case 'resizable':
                            if(newValue){
                                this.elements.content.removeAttribute('style');
                                iframe && iframe.removeAttribute('style');
                            }else{
                                this.elements.content.style.minHeight = 'inherit';
                                iframe && (iframe.style.minHeight = 'inherit');
                            }
                        break;    
                    }    
                }
            }
        };
    });
    //show the dialog
    alertify.YoutubeDialog('GODhPuM5cEE').set({frameless:true});
    <!-- JavaScript -->
    <script src="//cdn.jsdelivr.net/npm/alertifyjs@1.13.1/build/alertify.min.js"></script>
    <!-- CSS -->
    <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/alertifyjs@1.13.1/build/css/alertify.min.css"/>
    <!-- Default theme -->
    <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/alertifyjs@1.13.1/build/css/themes/default.min.css"/>
    <!-- Default theme -->
    <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/alertifyjs@1.13.1/build/css/themes/default.rtl.min.css"/>

    0 讨论(0)
  • 2020-11-22 02:33

    Not possible. If you want to customize the dialog's visual appearance, you need to use a JS-based solution like jQuery.UI dialog.

    0 讨论(0)
  • 2020-11-22 02:35

    I don't think you could change the style of browsers' default alert boxes.

    You need to create your own or use a simple and customizable library like xdialog. Following is a example to customize the alert box. More demos can be found here.

    function show_alert() {
        xdialog.alert("Hello! I am an alert box!");
    }
    <head>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/xxjapp/xdialog@3/xdialog.min.css"/>
        <script src="https://cdn.jsdelivr.net/gh/xxjapp/xdialog@3/xdialog.min.js"></script>
        
        <style>
            .xd-content .xd-body .xd-body-inner {
                max-height: unset;
            }
            .xd-content .xd-body p {
                color: #f0f;
                text-shadow: 0 0 5px rgba(0, 0, 0, 0.75);
            }
            .xd-content .xd-button.xd-ok {
                background: #734caf;
            }
        </style>
    </head>
    <body>
        <input type="button" onclick="show_alert()" value="Show alert box" />
    </body>

    0 讨论(0)
  • 2020-11-22 02:35

    I use sweetalert2 library. It's really simple, a lot of customization, modern, animated windows, eye-catching, and also nice design.

    Swal.fire({
      icon: 'error',
      title: 'Oops...',
      text: 'Something went wrong!',
      footer: '<a href>Why do I have this issue?</a>'
    })
    

    Check this link

    0 讨论(0)
  • 2020-11-22 02:36

    <head>
      
    
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
      <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
      
        <script type="text/javascript">
      
    
    $(function() {
        $( "#dialog" ).dialog({
          autoOpen: false,
          show: {
            effect: "blind",
            duration: 1000
          },
          hide: {
            effect: "explode",
            duration: 1000
          }
        });
     
        $( "#opener" ).click(function() {
          $( "#dialog" ).dialog( "open" );
        });
      });
        </script>
    </head>
    <body>
       <div id="dialog" title="Basic dialog">
       <p>This is an animated dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
      </div>
     
      <button id="opener">Open Dialog</button>
    
    </body>

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