我想在对话框上显示消息一段时间后重定向到新网页。 我的代码无法正常工作,请纠正我。当我在本地运行时看不到对话框。
test.html
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
if ($("#alert").length) {
var title;
if ($("#alert span").length) {
title = $("#alert span").text();
}
$("#alert div").dialog({
title: title,
modal: true,
open: function() { var foo = $(this);
setTimeout(function() {
foo.dialog('close');
}, 2000);
window.location.href = "http://stackoverflow.com";
}
});
}
});
</script>
<body>
<div id="alert">
<span>Password change</span>
<div>Password was successfully changed.</div>
</div>
</body>
<html>
最佳答案
您需要包含 jQuery-UI 才能使用对话框功能。
$(document).ready(function() {
if ($("#alert").length) {
var title;
if ($("#alert span").length) {
title = $("#alert span").text();
}
$("#alert div").dialog({
title: title,
modal: true,
open: function() {
var foo = $(this);
setTimeout(function() {
foo.dialog('close');
}, 2000);
//window.location.href = "http://stackoverflow.com";
}
});
}
});
<html>
<head>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
</head>
<body>
<div id="alert">
<span>Password change</span>
<div>Password was successfully changed.</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<!- I've added this. ->
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
</body>
<html>
关于javascript - 测试时 jquery 无法在本地运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37710034/