jQuery click on or off [duplicate]
Possible Duplicate:
jQuery bind click ANYTHING but ELEMENT
I have this HTML
<div id="outer">
<input id="input1" type="text" value="button1"/>
<input id="input2" type="text" value="button2"/>
</div>
And I am trying to use this
var insideOuter = "";
jQuery('#outer').live('click', function (e) {
if(insideOuter != "" && insideOuter != 'outer'){
alert('Inside');
}
insideOuter = "outer";
return false;
});
jQuery('body').live('click', function (e) {
if(insideOute开发者_运维问答r != ""&& insideOuter != 'body'){
alert('Outside');
}
insideOuter = "body";
return false;
});
Basically, I am trying to
- If user clicks inside
id="outer"
then do nothing else - If user clicks outside
id="outer"
do something
Can someone recommend something better ?
I'd recommend not using live() if you are not trying to bind events to elements that don't already exist in the DOM. Use bind() or click() instead.
Also, instead of checking for non-empty strings, use the length property:
if (insideOuter.length > 0 && ...
$("body").live('click', function (e) {
if ( $(e.srcElement).attr("id") !== 'outer' && $(e.srcElement).parents("#outer").length == 0)
alert('foo');
});
This is a duplicate question from one asked recently...I'll find that answer and post a link.
$(document).bind('click', function (e) {
// Do whatever you want; #outer click event has been stopped
});
$('#outer').bind('click', function(e) {
e.stopPropagation();
});
jQuery bind click *ANYTHING* but *ELEMENT*
How do I detect a click outside an element?
精彩评论