Как реализовать Toastr JS?
Я новичок в JS и не знаю, как сделать эту работу на моей странице. Ниже приводится то, что у меня есть. Как я могу сделать это оповещение?
Я правильно добавил источник, но не уверен, как сделать оповещение.
<!doctype html>
<html>
<head>
<title>Toast</title>
<link href="toastr.css" rel="stylesheet"/>
<script src="toastr.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
$(document).ready(function() {
//toastr.info('Are you the 6 fingered man?')
Command: toastr[success](" ", "Settings Saved!")
toastr.options: {
"debug": false,
"positionClass": "toast-top-right",
"onclick": null,
"fadeIn": 300,
"fadeOut": 1000,
"timeOut": 5000,
"extendedTimeOut": 1000
}
});
</script>
</head>
<body>
</body>
</html>
Ответы
Ответ 1
Toastr - очень хороший компонент, и вы можете показывать сообщения с помощью следующих команд:
// for success - green box
toastr.success('Success messages');
// for errors - red box
toastr.error('errors messages');
// for warning - orange box
toastr.warning('warning messages');
// for info - blue box
toastr.info('info messages');
Если вы хотите указать заголовок в сообщении toastr, просто добавьте второй аргумент:
// for info - blue box
toastr.success('The process has been saved.', 'Success');
Вы также можете изменить поведение по умолчанию, используя что-то вроде этого:
toastr.options.timeOut = 3000; // 3s
Смотрите больше на GitHub проекта.
Правки
Образец использования:
$(document).ready(function() {
// show when page load
toastr.info('Page Loaded!');
$('#linkButton').click(function() {
// show when the button is clicked
toastr.success('Click Button');
});
});
и HTML:
<a id='linkButton'>Show Message</a>
Ответ 2
Вам не нужно jquery-migrate. Суммируя предыдущие ответы, вот рабочий html:
<html>
<body>
<a id='linkButton'>ClickMe</a>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/css/toastr.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/js/toastr.js"></script>
<script type="text/javascript">
$(document).ready(function() {
toastr.options.timeOut = 1500; // 1.5s
toastr.info('Page Loaded!');
$('#linkButton').click(function() {
toastr.success('Click Button');
});
});
</script>
</body>
</html>
Ответ 3
Я исследую, я знал, что jquery script нужно загрузить, чтобы он не работал в вашем случае.
Поскольку символ $, упомянутый в коде, не понимается, если вы сначала загружаете JQuery 1.9.1.
Загрузите, как следует
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/css/toastr.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/js/toastr.js"></script>
Тогда он будет работать нормально
Ответ 4
Вы можете попробовать Msg, библиотеку для создания модальных окон и всплывающих окон/тостов. Он хорошо документирован и имеет множество возможностей.
Ответ 5
Добавить CDN файлы toastr.css и toastr.js
<link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/css/toastr.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/js/toastr.js"></script>
function toasterOptions() {
toastr.options = {
"closeButton": false,
"debug": false,
"newestOnTop": false,
"progressBar": true,
"positionClass": "toast-top-center",
"preventDuplicates": true,
"onclick": null,
"showDuration": "100",
"hideDuration": "1000",
"timeOut": "5000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "show",
"hideMethod": "hide"
};
};
toasterOptions();
toastr.error("Error Message from toastr");
Ответ 6
Это простой способ сделать это!
<link href="#" onclick="location.href='https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/css/toastr.css'; return false;" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/js/toastr.js"></script>
<script>
function notificationme(){
toastr.options = {
"closeButton": false,
"debug": false,
"newestOnTop": false,
"progressBar": true,
"preventDuplicates": true,
"onclick": null,
"showDuration": "100",
"hideDuration": "1000",
"timeOut": "5000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "show",
"hideMethod": "hide"
};
toastr.info('MY MESSAGE!');
}
</script>