Dynamically create Bootstrap alerts box through JavaScript

前端 未结 10 890
有刺的猬
有刺的猬 2020-12-13 12:20

I\'m using Bootstrap 2.0 for my project and I would like to dynamically add Bootstrap alert box in my page (http://twitter.github.com/bootstrap/javascript.html#alerts). I wa

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

    I ended up using toastr (https://github.com/CodeSeven/toastr) with style modifications Make toastr alerts look like bootstrap alerts

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

    I created this VERY SIMPLE and basic plugin:

    (function($){
        $.fn.extend({
            bs_alert: function(message, title){
                var cls='alert-danger';
                var html='<div class="alert '+cls+' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
                if(typeof title!=='undefined' &&  title!==''){
                    html+='<h4>'+title+'</h4>';
                }
                html+='<span>'+message+'</span></div>';
                $(this).html(html);
            },
            bs_warning: function(message, title){
                var cls='alert-warning';
                var html='<div class="alert '+cls+' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
                if(typeof title!=='undefined' &&  title!==''){
                    html+='<h4>'+title+'</h4>';
                }
                html+='<span>'+message+'</span></div>';
                $(this).html(html);
            },
            bs_info: function(message, title){
                var cls='alert-info';
                var html='<div class="alert '+cls+' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
                if(typeof title!=='undefined' &&  title!==''){
                    html+='<h4>'+title+'</h4>';
                }
                html+='<span>'+message+'</span></div>';
                $(this).html(html);
            }
        });
    })(jQuery);
    

    Usage is

    <div id="error_container"></div>
    <script>
    $('#error_container').bs_alert('YOUR ERROR MESSAGE HERE !!', 'title');
    </script>
    

    first plugin EVER and it can be easily made better

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

    FWIW I created a JavaScript class that can be used at run-time.
    It's over on GitHub, here.

    There is a readme there with a more in-depth explanation, but I'll do a quick example below:

    var ba = new BootstrapAlert();
    ba.addP("Some content here");
    $("body").append(ba.render());
    

    The above would create a simple primary alert with a paragraph element inside containing the text "Some content here".

    There are also options that can be set on initialisation.
    For your requirement you'd do:

    var ba = new BootstrapAlert({
        dismissible: true,
        background: 'warning'
    });
    ba.addP("Invalid Credentials");
    $("body").append(ba.render());
    

    The render method will return an HTML element, which can then be inserted into the DOM. In this case, we append it to the bottom of the body tag.

    This is a work in progress library, but it still is in a very good working order.

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

    You can also create a HTML alert template like this:

    <div class="alert alert-info" id="alert_template" style="display: none;">
        <button type="button" class="close">×</button>
    </div>
    

    And so you can do in JavaScript this here:

    $("#alert_template button").after('<span>Some text</span>');
    $('#alert_template').fadeIn('slow');
    

    Which is in my opinion cleaner and faster. In addition you stick to Twitter Bootstrap standards when calling fadeIn().

    To guarantee that this alert template works also with multiple calls (so it doesn't add the new message to the old one), add this here to your JavaScript:

    $('#alert_template .close').click(function(e) {
        $("#alert_template span").remove();
    });
    

    So this call removes the span element every time you close the alert via the x-button.

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