Dynamically create Bootstrap alerts box through JavaScript

前端 未结 10 912
有刺的猬
有刺的猬 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条回答
  •  萌比男神i
    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.

提交回复
热议问题