I need to run funct开发者_StackOverflowion: hideLogin(event)
when the user clicks (onmousedown
actually) anywhere except
<div id="loginForm">
How can I do this? Thanks
Using jQuery:
$('body *').click(function() {
if (this.id != 'loginForm') {
hideLogin();
}
});
Without jQuery:
<body onmousedown="onBodyClick()">
Code:
function onBodyClick() {
if (this.id != 'loginForm') {
hideLogin();
}
}
You may view a working example here: http://jsfiddle.net/qMEJR/.
The example creates 100 divs, with the 10th one having an ID of 'loginForm'. Clicking on any div
except for the one with the id of loginForm will alert the ID of the div
clicked.
JavaScript (jQuery):
for(var i=0;i<100;i++){
if(i==10){
$("body").append('<div id="loginForm">LoginForm</div>');
}else{
$("body").append('<div id="div-'+i+'">'+i+'</div>');
}
}
$("div").click(function(){
var obj = $(this);
var objID = obj.attr("id");
if(objID != "loginForm"){
alert(obj.attr("id")); // Replace with function here
}
});
Why not let the function run when the user clicks anywhere, and add a condition so that it doesn't execute any code if the element is loginForm?
精彩评论