javascript - 测试时 jquery 无法在本地运行

标签 javascript jquery html

我想在对话框上显示消息一段时间后重定向到新网页。 我的代码无法正常工作,请纠正我。当我在本地运行时看不到对话框。

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/

相关文章:

javascript - 当用户单击 Safari 扩展的工具栏命令时获取键盘修饰符

javascript - 隐藏 div 的高度不会随 Masonry 内容扩展

javascript - HTML 间距问题 - 所有浏览器

javascript - 使背景变为空白并重定向 IE 8 及更早版本

html - Twitter Bootstrap Modal 未出现在用户 View 中

html - Bootstrap,无法将我的图像和文本并排放置在 3 组中

javascript - 在 Three.js 中向场景添加相机的原因?

javascript - JavaScript 中的日期操作

javascript - jQuery - 有没有任何类(class)的 child (不是特定类(class))

jquery - 推特 Bootstrap : how to add x on the right for clearing inside input box when user is typing?