开发者

Basic action on click

For some reason, I can't get jQuery to do what I want it to do.

What I need is for the container div to disappear when one of its children is clicked.

Here's example HTML

<div id="container">
 <a href="link">
  <div id="ch开发者_Go百科ild1">
   When this is clicked, #container disappears,
   including everything contained inside...
  </div>
 </a>

 <a href="link">
  <div id="child2">
   ...or when this one is clicked
  </div>
 </a>
</div>

Here's what I've tried.

$("#child1").click(function () { 
  $(#container).hide(); 
});

and

$("#child1").click(function () { 
  $(#container).fadeOut("fast"); 
});

Thanks in advance.


With this code, no matter how many elements you have in your #container div, clicking on them will hide the whole div :

$("#container").children().bind("click", function(e){
    e.preventDefault();
   $("#container").fadeOut();
});

See here : http://jsfiddle.net/pioul/Bd9Dc/


$("#child1, #child2").click(function(e) {
    $(e.target).parents("#container").hide();
}

Something like that? Or more like

$("#container").children("div").click(function(e) {
    $(e.target).parents("#container").hide();
}


$("#container").children().click(function(e) {
    $("#container").hide();
}


From what you've posted, your selectors are not valid strings.
$(#container) will throw a syntax error, because the syntax should be $(selector) where selector is a string or an object.
So, just update your selectors from

$(#container)

to

$('#container')

Note : even this site's syntax highlighter gives you a hint that something is wrong!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜