思路:
通过样式表实现DIV块的显示或消失
在DIV块中定义一个按钮,按钮的onclick事件用JavaScript实现对DIV块CSS属性的操作。
base.html中,显示flash消息的代码:
<!-- 前面代码略去 -->
<script>
function close_flash_message(){
document.all.alertmessage.style.display='none';
return false;
}
</script>
<!--中间代码略去-->
{% for message in get_flashed_messages()%}
<div id="alertmessage" class = "alert alert-warning">
<button type="button" class = "close" οnclick="close_flash_message();">x</button>
{{ message }}
</div>
{% endfor %}
css样式表:
.alert {
padding: 15px;
margin-bottom: 20px;
border: 1px solid transparent;
border-radius: 4px;
}
.alert-warning {
color: #8a6d3b;
background-color: #fcf8e3;
border-color: #faebcc;
}
.close {
float: right;
font-size: 21px;
font-weight: bold;
line-height: 1;
color: #000;
text-shadow: 0 1px 0 #fff;
filter: alpha(opacity=20);
opacity: .2;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
filter: alpha(opacity=50);
opacity: .5;
}
显示效果: