开发者

Do not hide specific childnodes onclick a parent

I've got the next HTML code:

<div id="main-container">
  <article id="doyou">...</article开发者_JAVA技巧>
  <article id="theydid">...</article>
  <article id="nieuws">...</article>
  ...
</div>

I'm trying to hide the articles when clicked on the div outside the articles, but not when the articles itself are clicked. Currently I got the following code, but it ain't working:

code:

$("#main-container").click(function(){
        $('article#doyou').not(this).hide();
        $('article#theydid').not(this).hide();
        $('article#nieuws').not(this).hide();
        $('article#stage').not(this).hide();
        $('article#info').not(this).hide();
        $('article#contact').not(this).hide();
        $('article#letop').not(this).hide();
    });


First of all I'd give each article a common class:

<div id="main-container">
    <article id="doyou" class="article">...</article>
    <article id="theydid" class="article">...</article>
    <article id="nieuws" class="article">...</article>
    ... other articles ...
</div>

Then in jQuery you can use that class to hide all except the clicked element:

$(function() {
    $(".article").click(function() {
        hideArticles();
        $(this).show();
    });

    $("#main-container").click(function(e) {
        if (e.target.id == "main-container")
            hideArticles();
    });
});

function hideArticles() {
    $(".article").hide();
}

As I mentioned in my comment though, there is no mechanism for displaying all the articles again. Once an article has been clicked, all others will not be recoverable.

You may want to add a 'show articles' button:

$("#show-articles").click(function() {
    $(".article").show();
});


You can fix like this

$(document).ready(function() {
    $("#main-container").click(function(e) {
        $('article#doyou').not(e.target).hide();
        $('article#theydid').not(e.target).hide();
        $('article#nieuws').not(e.target).hide();
        $('article#stage').not(e.target).hide();
        $('article#info').not(e.target).hide();
        $('article#contact').not(e.target).hide();
        $('article#letop').not(e.target).hide();
    });
});

Demo: http://jsfiddle.net/ynhat/5uuLc/1/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜