只执行一次jquery
有人能帮我吗?我这里有一个脚本,当鼠标离开页面时会显示一个对话框,但我只希望它执行一次,在第一次之后,我不希望它再次发生。 这是我的代码:只执行一次jquery,jquery,dialog,Jquery,Dialog,有人能帮我吗?我这里有一个脚本,当鼠标离开页面时会显示一个对话框,但我只希望它执行一次,在第一次之后,我不希望它再次发生。 这是我的代码: <html> <head> <style> #dialog { width:652px; margin-right:auto; margin-left:auto; display:none; } </style> <script src="http://code.jque
<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>
#对话{
宽度:652px;
右边距:自动;
左边距:自动;
显示:无;
}
函数显示图像(src、宽度、高度、高度){
var img=document.createElement(“img”);
img.src=src;
img.width=宽度;
img.height=高度;
img.alt=alt;
//下一行只是将其添加到标记中
文件.正文.附件(img);
}
var计数=0;
功能加法器(obj、evt、fn){
if(对象添加列表器){
对象添加列表器(evt、fn、false);
}
else if(对象附件){
附件(“on”+evt,fn);
}
}
addEvent(窗口,“加载”,功能(e){
附录(文件“鼠标输出”,功能(e){
e=e?e:window.event;
var from=e.相关目标| | e.元素;
如果(!from | | from.nodeName==“HTML”){
//在此处停止拖动事件
//现在我们可以使用警报
如果(计数<1){
$(函数(){
var计数=1;
$(“#对话框”)。对话框({
宽度:692
});
});
}
}
});
});
任何帮助都会很好!谢谢。您有两个选择:
1\u解除绑定执行此操作的事件(需要为此使用jquery事件)
2_使用更糟糕的方法,并使用这样的标志
var flag=1;
addEvent(window,"load",function(e) {
addEvent(document, "mouseout", function(e) {
if(flag){
flag=0;
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
});
});
}
}
}
});
});
var标志=1;
addEvent(窗口,“加载”,功能(e){
附录(文件“鼠标输出”,功能(e){
国际单项体育联合会(旗){
flag=0;
e=e?e:window.event;
var from=e.相关目标| | e.元素;
如果(!from | | from.nodeName==“HTML”){
//在此处停止拖动事件
//现在我们可以使用警报
如果(计数<1){
$(函数(){
var计数=1;
$(“#对话框”)。对话框({
宽度:692
});
});
}
}
}
});
});
您需要有计数=1代码>
而不是var count=1代码>(~47行)
而count=0代码>而不是var count=0代码>(第29行)
第29行(var count=0;
)定义了变量“count”,但不能在代码中重写或增加count,因为它不在同一范围内。复制/粘贴此代码,并查看ti是否适用于您:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
#dialog {
width:652px;
margin-right:auto;
margin-left:auto;
display:none;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
var count = 0;
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);
}
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() {
count = 1;
$( "#dialog" ).dialog({
width:692
});
});
}
}
});
});
}); //END $(document).ready()
</script>
</head>
<body>
<div id="dialog">
<img src="http://www.maxcashtitleloans.com/POPUPIMAGE.jpg">
</div>
</body>
</html>
#对话{
宽度:652px;
右边距:自动;
左边距:自动;
显示:无;
}
$(文档).ready(函数(){
var计数=0;
函数显示图像(src、宽度、高度、高度){
var img=document.createElement(“img”);
img.src=src;
img.width=宽度;
img.height=高度;
img.alt=alt;
//下一行只是将其添加到标记中
文件.正文.附件(img);
}
功能加法器(obj、evt、fn){
if(对象添加列表器){
对象添加列表器(evt、fn、false);
}
else if(对象附件){
附件(“on”+evt,fn);
}
}
addEvent(窗口,“加载”,功能(e){
附录(文件“鼠标输出”,功能(e){
e=e?e:window.event;
var from=e.相关目标| | e.元素;
如果(!from | | from.nodeName==“HTML”){
//在此处停止拖动事件
//现在我们可以使用警报
如果(计数<1){
$(函数(){
计数=1;
$(“#对话框”)。对话框({
宽度:692
});
});
}
}
});
});
}); //END$(document.ready())
将代码更改为使用jQuery进行事件处理,并使用.one()
函数仅执行一次处理程序。看起来您也可以在全局范围内而不是在事件处理程序内声明count
。我无法确定鼠标事件句柄的jquery。您的代码没有引用jQueryUI样式表。这对您的问题并不重要,但以后可能会很重要(没有它,您的对话框将无法工作)。有关示例,请参阅的最后一部分。此代码应适用于您:。同时,它也是一个很好的资源。
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
#dialog {
width:652px;
margin-right:auto;
margin-left:auto;
display:none;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
var count = 0;
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);
}
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() {
count = 1;
$( "#dialog" ).dialog({
width:692
});
});
}
}
});
});
}); //END $(document).ready()
</script>
</head>
<body>
<div id="dialog">
<img src="http://www.maxcashtitleloans.com/POPUPIMAGE.jpg">
</div>
</body>
</html>