Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
jquery
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
夏彬
jquery
Commits
37c237af
Commit
37c237af
authored
Jun 19, 2006
by
John Resig
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
I've cleaned up and documented the events plugin, it's close to being ready for 1.0.
parent
7dd68a08
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
163 additions
and
107 deletions
+163
-107
event.js
event/event.js
+163
-107
No files found.
event/event.js
View file @
37c237af
// We're overriding the old toggle function, so
// remember it for later
$
.
fn
.
_toggle
=
$
.
fn
.
toggle
;
/**
* Toggle between two function calls every other click.
*/
$
.
fn
.
toggle
=
function
(
a
,
b
)
{
// If two functions are passed in, we're
// toggling on a click
return
a
&&
b
?
this
.
click
(
function
(
e
){
// Figure out which function to execute
this
.
last
=
this
.
last
==
a
?
b
:
a
;
// Make sure that clicks don't pass through
e
.
preventDefault
();
// and execute the function
return
$
.
apply
(
this
,
this
.
last
,
[
e
]
)
||
false
;
})
:
// Otherwise, execute the old toggle function
this
.
_toggle
();
};
/**
* Toggle between two function calls on mouse over/out.
*/
$
.
fn
.
hover
=
function
(
f
,
g
)
{
// A private function for haandling mouse 'hovering'
function
handleHover
(
e
)
{
// Check if mouse(over|out) are still within the same parent element
var
p
=
e
.
fromElement
||
e
.
toElement
||
e
.
relatedTarget
;
while
(
p
&&
p
!=
this
)
p
=
p
.
parentNode
;
// If we actually just moused on to a sub-element, ignore it
if
(
p
==
this
)
return
false
;
// Execute the right function
return
$
.
apply
(
this
,
e
.
type
==
'mouseover'
?
f
:
g
,[
e
]);
}
// Bind the function to the two event listeners
return
this
.
mouseover
(
handleHover
).
mouseout
(
handleHover
);
};
/**
* Bind a function to fire when the DOM is ready.
*/
$
.
fn
.
ready
=
function
(
f
)
{
// If the DOM is already ready
if
(
$
.
isReady
)
// Execute the function immediately
$
.
apply
(
document
,
f
);
// Otherwise, remember the function for later
else
{
// Add the function to the wait list
$
.
readyList
.
push
(
f
);
}
return
this
;
};
(
function
(){
(
function
(){
/*
* Bind a number of event-handling functions, dynamically
*/
var
e
=
[
"blur"
,
"focus"
,
"contextmenu"
,
"load"
,
"resize"
,
"scroll"
,
"unload"
,
var
e
=
[
"blur"
,
"focus"
,
"contextmenu"
,
"load"
,
"resize"
,
"scroll"
,
"unload"
,
"click"
,
"dblclick"
,
"mousedown"
,
"mouseup"
,
"mouseenter"
,
"mouseleave"
,
"click"
,
"dblclick"
,
"mousedown"
,
"mouseup"
,
"mouseenter"
,
"mouseleave"
,
"mousemove"
,
"mouseover"
,
"mouseout"
,
"change"
,
"reset"
,
"select"
,
"submit"
,
"mousemove"
,
"mouseover"
,
"mouseout"
,
"change"
,
"reset"
,
"select"
,
"submit"
,
"keydown"
,
"keypress"
,
"keyup"
,
"abort"
,
"error"
,
"ready"
];
"keydown"
,
"keypress"
,
"keyup"
,
"abort"
,
"error"
,
"ready"
];
for
(
var
i
=
0
;
i
<
e
.
length
;
i
++
)
{
// Go through all the event names, but make sure that
(
function
(){
// it is enclosed properly
for
(
var
i
=
0
;
i
<
e
.
length
;
i
++
)
{(
function
(){
var
o
=
e
[
i
];
var
o
=
e
[
i
];
// Handle event binding
$
.
fn
[
o
]
=
function
(
f
){
return
this
.
bind
(
o
,
f
);
};
$
.
fn
[
o
]
=
function
(
f
){
return
this
.
bind
(
o
,
f
);
};
// Handle event unbinding
$
.
fn
[
"un"
+
o
]
=
function
(
f
){
return
this
.
unbind
(
o
,
f
);
};
$
.
fn
[
"un"
+
o
]
=
function
(
f
){
return
this
.
unbind
(
o
,
f
);
};
// Handle event triggering
$
.
fn
[
"do"
+
o
]
=
function
(){
return
this
.
trigger
(
o
);
};
$
.
fn
[
"do"
+
o
]
=
function
(){
return
this
.
trigger
(
o
);
};
$
.
fn
[
"one"
+
o
]
=
function
(
f
){
return
this
.
bind
(
o
,
function
(
e
){
if
(
this
[
o
+
f
]
!==
null
)
{
return
true
;
}
// Finally, handle events that only fire once
$
.
fn
[
"one"
+
o
]
=
function
(
f
){
// Attach the event listener
return
this
.
bind
(
o
,
function
(
e
){
// TODO: Remove the event listener, instead of this hack
// If this function has already been executed, stop
if
(
this
[
o
+
f
]
!==
null
)
return
true
;
// Otherwise, mark as having been executed
this
[
o
+
f
]
++
;
this
[
o
+
f
]
++
;
// And execute the bound function
return
$
.
apply
(
this
,
f
,[
e
]);
return
$
.
apply
(
this
,
f
,[
e
]);
});
};
});
};
// Deprecated
})();}
//$.fn["on"+o] = function(f){ return this.bind(o, f); };
})();
}
})();
$
.
fn
.
hover
=
function
(
f
,
g
)
{
/*
// Check if mouse(over|out) are still within the same parent element
* All the code that makes DOM Ready work nicely.
return
this
.
each
(
function
(){
*/
var
obj
=
this
;
$
.
event
.
add
(
this
,
"mouseover"
,
function
(
e
)
{
var
p
=
(
e
.
fromElement
!==
null
?
e
.
fromElement
:
e
.
relatedTarget
);
while
(
p
&&
p
!=
obj
)
{
p
=
p
.
parentNode
;
}
if
(
p
==
obj
)
{
return
false
;
}
return
$
.
apply
(
obj
,
f
,[
e
]);
});
$
.
event
.
add
(
this
,
"mouseout"
,
function
(
e
)
{
var
p
=
(
e
.
toElement
!==
null
?
e
.
toElement
:
e
.
relatedTarget
);
while
(
p
&&
p
!=
obj
)
{
p
=
p
.
parentNode
;
}
if
(
p
==
obj
)
{
return
false
;
}
return
$
.
apply
(
obj
,
g
,[
e
]);
});
});
};
$
.
$$
isReady
=
false
;
$
.
isReady
=
false
;
$
.
$$ready
=
[];
$
.
readyList
=
[];
// Handle when the DOM is ready
// Handle when the DOM is ready
$
.
ready
=
function
()
{
$
.
ready
=
function
()
{
if
(
!
$
.
$$isReady
)
{
// Make sure that the DOM hasn't already loaded
$
.
$$isReady
=
true
;
if
(
!
$
.
isReady
)
{
if
(
$
.
$$ready
)
{
// Remember that the DOM is ready
for
(
var
i
=
0
;
i
<
$
.
$$ready
.
length
;
i
++
)
{
$
.
isReady
=
true
;
$
.
apply
(
document
,
$
.
$$ready
[
i
]
);
}
// If there are functions bound, to execute
$
.
$$ready
=
[];
if
(
$
.
readyList
)
{
// Execute all of them
for
(
var
i
=
0
;
i
<
$
.
readyList
.
length
;
i
++
)
$
.
apply
(
document
,
$
.
readyList
[
i
]
);
// Reset the list of functions
$
.
readyList
=
null
;
}
}
}
}
};
};
// If Mozilla is used
// If Mozilla is used
if
(
$
.
browser
==
"mozilla"
||
$
.
browser
==
"opera"
)
{
if
(
$
.
browser
==
"mozilla"
||
$
.
browser
==
"opera"
)
{
// Use the handy event callback
// Use the handy event callback
document
.
addEventListener
(
"DOMContentLoaded"
,
$
.
ready
,
null
);
$
.
event
.
add
(
document
,
"DOMContentLoaded"
,
$
.
ready
);
// If IE is used, use the excellent hack by Matthias Miller
// If IE is used, use the excellent hack by Matthias Miller
// http://www.outofhanwell.com/blog/index.php?title=the_window_onload_problem_revisited
// http://www.outofhanwell.com/blog/index.php?title=the_window_onload_problem_revisited
}
else
if
(
$
.
browser
==
"msie"
)
{
}
else
if
(
$
.
browser
==
"msie"
)
{
// Only works if you document.write() it
// Only works if you document.write() it
document
.
write
(
'<scr'
+
'ipt id=__ie_init defer=true '
+
document
.
write
(
'<scr'
+
'ipt id=__ie_init defer=true '
+
...
@@ -73,52 +149,32 @@ if ( $.browser == "mozilla" || $.browser == "opera" ) {
...
@@ -73,52 +149,32 @@ if ( $.browser == "mozilla" || $.browser == "opera" ) {
// Use the defer script hack
// Use the defer script hack
var
script
=
document
.
getElementById
(
'__ie_init'
);
var
script
=
document
.
getElementById
(
'__ie_init'
);
script
.
onreadystatechange
=
function
()
{
script
.
onreadystatechange
=
function
()
{
if
(
this
.
readyState
==
'complete'
)
{
if
(
this
.
readyState
==
'complete'
)
$
.
ready
();
$
.
ready
();
}
};
};
// Clear from memory
// Clear from memory
script
=
null
;
script
=
null
;
// If Safari is used
// If Safari is used
}
else
if
(
$
.
browser
==
"safari"
)
{
}
else
if
(
$
.
browser
==
"safari"
)
{
$
.
$$timer
=
setInterval
(
function
(){
// Continually check to see if the document.readyState is valid
$
.
safariTimer
=
setInterval
(
function
(){
// loaded and complete are both valid states
if
(
document
.
readyState
==
"loaded"
||
if
(
document
.
readyState
==
"loaded"
||
document
.
readyState
==
"complete"
)
{
document
.
readyState
==
"complete"
)
{
clearInterval
(
$
.
$$timer
);
// If either one are found, remove the timer
$
.
$$timer
=
null
;
clearInterval
(
$
.
safariTimer
);
$
.
safariTimer
=
null
;
// and execute any waiting functions
$
.
ready
();
$
.
ready
();
}
}
},
10
);
},
10
);
}
// A fallback, that will always work, just in case
$
.
event
.
add
(
window
,
"load"
,
$
.
ready
);
/**
* Bind a function to fire when the DOM is ready.
*/
$
.
fn
.
ready
=
function
(
f
)
{
if
(
$
.
$$isReady
)
{
$
.
apply
(
document
,
f
);
}
else
{
if
(
!
$
.
$$ready
)
{
$
.
$$ready
=
[];
}
}
$
.
$$ready
.
push
(
f
);
// A fallback to window.onload, that will always work
}
$
.
event
.
add
(
window
,
"load"
,
$
.
ready
);
return
this
;
})();
};
$
.
fn
.
toggle
=
function
(
a
,
b
)
{
return
a
&&
b
?
this
.
click
(
function
(
e
){
this
.
$$last
=
this
.
$$last
==
a
?
b
:
a
;
e
.
preventDefault
();
return
$
.
apply
(
this
,
this
.
$$last
,
[
e
]
)
||
false
;
})
:
this
.
_toggle
();
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment