ed1*_*d1t 54 javascript twitter-bootstrap
我正在为我的项目使用Bootstrap 2.0,我想在我的页面中动态添加Bootstrap警报框(http://twitter.github.com/bootstrap/javascript.html#alerts).我想做的事情如下:
bootstrap-alert.warning("Invalid Credentials");
Run Code Online (Sandbox Code Playgroud)
per*_*lis 82
试试这个(参见jsfiddle中这个代码的一个工作示例:http://jsfiddle.net/periklis/7ATLS/1/)
<input type = "button" id = "clickme" value="Click me!"/>
<div id = "alert_placeholder"></div>
<script>
bootstrap_alert = function() {}
bootstrap_alert.warning = function(message) {
$('#alert_placeholder').html('<div class="alert"><a class="close" data-dismiss="alert">×</a><span>'+message+'</span></div>')
}
$('#clickme').on('click', function() {
bootstrap_alert.warning('Your text goes here');
});
</script>?
Run Code Online (Sandbox Code Playgroud)
编辑:现在有一些库可以简化和简化这个过程,比如bootbox.js
Ube*_*Neo 39
/**
Bootstrap Alerts -
Function Name - showalert()
Inputs - message,alerttype
Example - showalert("Invalid Login","alert-error")
Types of alerts -- "alert-error","alert-success","alert-info","alert-warning"
Required - You only need to add a alert_placeholder div in your html page wherever you want to display these alerts "<div id="alert_placeholder"></div>"
Written On - 14-Jun-2013
**/
function showalert(message,alerttype) {
$('#alert_placeholder').append('<div id="alertdiv" class="alert ' + alerttype + '"><a class="close" data-dismiss="alert">×</a><span>'+message+'</span></div>')
setTimeout(function() { // this will automatically close the alert and remove this if the users doesnt close it in 5 secs
$("#alertdiv").remove();
}, 5000);
}
Run Code Online (Sandbox Code Playgroud)
小智 9
您还可以创建如下的HTML警报模板:
<div class="alert alert-info" id="alert_template" style="display: none;">
<button type="button" class="close">×</button>
</div>
Run Code Online (Sandbox Code Playgroud)
所以你可以在这里用JavaScript做:
$("#alert_template button").after('<span>Some text</span>');
$('#alert_template').fadeIn('slow');
Run Code Online (Sandbox Code Playgroud)
在我看来哪个更清洁,更快.此外,您在通话时坚持使用Twitter Bootstrap标准fadeIn()
.
为了保证此警报模板也可以使用多个调用(因此它不会将新消息添加到旧消息中),请将此处添加到您的JavaScript中:
$('#alert_template .close').click(function(e) {
$("#alert_template span").remove();
});
Run Code Online (Sandbox Code Playgroud)
因此,每次通过x按钮关闭警报时,此调用都会删除span元素.
我创建了这个非常简单和基本的插件:
(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">×</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">×</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">×</button>';
if(typeof title!=='undefined' && title!==''){
html+='<h4>'+title+'</h4>';
}
html+='<span>'+message+'</span></div>';
$(this).html(html);
}
});
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
用法是
<div id="error_container"></div>
<script>
$('#error_container').bs_alert('YOUR ERROR MESSAGE HERE !!', 'title');
</script>
Run Code Online (Sandbox Code Playgroud)
第一个插件EVER,它可以很容易地变得更好
归档时间: |
|
查看次数: |
149770 次 |
最近记录: |