🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.javascript
Article #51170

Re: css js toggleVisibility

#51170
From: Roman Dobosz
Date: Tue, 04 Sep 2012 20:45
97 lines
3883 bytes
On Tue, 4 Sep 2012 03:42:53 -0700 (PDT)
BooB <max-in@wp.pl> wrote:

> są dwa obrazki . jak zrobić aby po kliknięciu drugiego obrazka chował się 
> div przypisany do pierwszego ?? i na odwrót.
> na razie to muszę kliknąć aby wpierw schować poprzedni div .
[wipe]

Onclicki, style inline, zepsute linki… Na zadanie domowe zastanów się
jak to naprawić, żeby działało BEZ włączonego javascriptu w
przeglądarce.

-- >8 ---
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>Js</title>
    <script>
        window.onload = function(){
            var $ = function(arg){
                return document.getElementById(arg);
            };

            function toggleVisibility(obj){
                var child = null;
                if (obj.parentNode.hasChildNodes())
                    var child = obj.parentNode.firstElementChild;

                while(child){
                    if (child.id != obj.id && child.classList.contains('on')){
                        child.classList.remove("on");
                        child.classList.add("off");
                    }
                    child = child.nextElementSibling;
                }
                obj.classList.remove("off");
                obj.classList.add("on");
            }


            var get_callback = function(elem_id){
                return function(){
                    toggleVisibility($(elem_id));
                }
            };

            var form = $("form");
            form.onsubmit = function(){ return false; };

            var child = null;

            if(form.hasChildNodes())
                child = form.firstElementChild;

            while(child){
                child.onclick = get_callback(child.getAttribute("data-rel"));
                child = child.nextElementSibling;
            }
        };
    </script>
    <style>
        .img {position: absolute; width: 100px; height: 100px; left: 300px; 
            top: 150px}
        #wr11 {background-color: yellow}
        #wr12 {background-color: green}
        #wr13 {background-color: red}
        #wr14 {background-color: blue}
        #wr15 {background-color: black}
        .on {visibility: visible}
        .off {visibility: hidden}
    </style>
</head>
<body>
    <div>
        <div id="wr11" class="img off"></div>
        <div id="wr12" class="img off"></div>
        <div id="wr13" class="img off"></div>
        <div id="wr14" class="img off"></div>
        <div id="wr15" class="img off"></div>
    </div>

    <form id="form" action="js.html">
        <button id="yellow" data-rel="wr11">yellow</button>
        <button id="green" data-rel="wr12">green</button>
        <button id="red" data-rel="wr13">red</button>
        <button id="blue" data-rel="wr14">blue</button>
        <button id="black" data-rel="wr15">black</button>
    </form>
</body>
</html>
-- >8 ---

-- 
      -^-  _        coś tam w tle sobie gra, np:
   _ /O)_\//        haujobb - Unseeing
  (_(|__(_(_) grf.  
Technical Details
Message-ID: <20120904224536.b8535f14.gryf_esm-no@spam-o2.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!.POSTED!not-for-mail
References: <9a685326-7dbb-4416-acff-ddef531ae8b9@googlegroups.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!.POSTED!not-for-mail
From: Roman Dobosz <gryf_esm-no@spam-o2.pl>
Newsgroups: pl.comp.lang.javascript
Subject: Re: css js toggleVisibility
Date: Tue, 4 Sep 2012 22:45:36 +0200
Organization: ICM, Uniwersytet Warszawski
Lines: 97
Message-ID: <20120904224536.b8535f14.gryf_esm-no@spam-o2.pl>
References: <9a685326-7dbb-4416-acff-ddef531ae8b9@googlegroups.com>
NNTP-Posting-Host: ecb246.neoplus.adsl.tpnet.pl
Mime-Version: 1.0
X-Trace: news.icm.edu.pl 1346791536 15203 83.22.217.246 (4 Sep 2012 20:45:36 GMT)
X-Complaints-To: usenet@news.icm.edu.pl
NNTP-Posting-Date: Tue, 4 Sep 2012 20:45:36 +0000 (UTC)
X-Newsreader: Sylpheed 3.1.4 (GTK+ 2.24.10; i686-pc-linux-gnu)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit