jquery - 只执行一次jquery

标签 jquery dialog

任何人都可以帮助我,我这里有一个脚本,当你的鼠标离开页面时显示一个对话框,但我只希望它执行一次,在第一次之后,我不希望它再次发生。 这是我的代码:

<html>
<head>
<style>
#dialog {
    width:652px;
    margin-right:auto;
    margin-left:auto;
    display:none;
}
</style>
 <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>

function show_image(src, width, height, alt) {
    var img = document.createElement("img");
    img.src = src;
    img.width = width;
    img.height = height;
    img.alt = alt;

    // This next line will just add it to the <body> tag
    document.body.appendChild(img);
}

</script>
<script type="text/javascript">
var count = 0;
function addEvent(obj, evt, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evt, fn, false);
    }
    else if (obj.attachEvent) {
        obj.attachEvent("on" + evt, fn);
    }
}
addEvent(window,"load",function(e) {
    addEvent(document, "mouseout", function(e) {
        e = e ? e : window.event;
        var from = e.relatedTarget || e.toElement;
        if (!from || from.nodeName == "HTML") {
            // stop your drag event here
            // for now we can just use an alert
            if (count < 1) {
              $(function() {
                var count = 1;
                $( "#dialog" ).dialog({
                width:692
                });
                });
            }
        }
    });
});
</script>
</head>
<body>
<div id="dialog">
<img src="http://www.maxcashtitleloans.com/POPUPIMAGE.jpg">
</div>
</body>
</html>

任何帮助都会很棒!谢谢。

最佳答案

您需要有count = 1;

而不是var count = 1;(~第47行)

count = 0; 而不是 var count = 0;(第 29 行)

第 29 行 (var count = 0;) 定义了变量“count”,但无法在代码中覆盖或增加 count,因为它不在同一范围内。

关于jquery - 只执行一次jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20527170/

相关文章:

javascript - 更平滑的 JQuery 宽度变化

ios - 使用 FBConnect 预填充对话框?

python - 如何将 "dialog"输出流式传输到标准输出?

android - 设置 Android 对话框位置

jquery - 在响应式菜单中显示事件链接

选项转换器的 Javascript 改进

javascript - Jquerymobile 选择菜单禁用选项

javascript - jquery 无法从隐藏字段中获取值

internet-explorer - IE 中的 ActiveX。对话框不显示 XP 视觉样式

android - 通过 IntentService 显示对话框?