我使用引導程序3發出了兩條警報消息。每條警報均爲div,並帶有類警報。JavaScript中的document.getElementsByClassName
我想在類添加褪色和到每個警報,使他們結束時消失。
爲什麼下面的JS腳本不起作用?謝謝。
var alerts = document.getElementsByClassName("alert");
for (var i = 0; i < alerts.length; i++) {
alerts[i].className += "fade in";
}
我試過使用jQuery和那工作。
$(document).ready(function() {
$('.alert').addClass('fade in');
});
故障代碼:
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script type="text/javascript">
var alerts = document.getElementsByClassName("alert");
for (var i = 0; i < alerts.length; i++) {
alerts[i].className += "fade in";
}
</script>
</head>
<body>
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
Message</div>
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
Message</div>
</body>
</html>