Is there a cross browser way to expand text when css is clicked?

First, here is what I would like to do:

Row 1
Row 2
Row 3

I have a code setting so that when you hover over line 1, 2 or 3, the line is highlighted. This is done via css.

I would like to be able (for example) to click row1, and then it will look like this:

Row 1
  Some text here
Row 2
Row 3

This text will remain there until I click on another line, at which point it will disappear. For example, let's say I pressed the following line 2.

Row 1
Row 2
  Even more text here
Row 3

I found code that talks about using javascript to do this, and setting the visibility of the text, but I'm not sure how I can do this without having a ton of nearly duplicated code ...

+3
5

javascript, , jQuery , . jQuery -

$(".classOfYourRows").
    click(function(){
        $(".classOfChildren").hide();
        $(".classOfChildren", this).show();

    });
+7

CSS

.outer {
  display: block;
}

.outer .inner {
  display: none;
  margin-left: 10px;
}

.outer:hover {
  background-color: #EEEEEE;
}

.outer:active .inner {
  display: block;
}

HTML:

<div class="outer">
    Row 1
    <div class="inner">some text</div>
</div>
<div class="outer">
    Row 2
    <div class="inner">some text</div>
</div>
<div class="outer">
    Row 3
    <div class="inner">some text</div>
</div>

, , Javascript.

: , , , , , Javascript. , .

+3

- - Javascript, , , : -, .

, MooTools Fx.Slide , , , JQuery.

+2

, , jQuery , , OP , JavaScript-, FF3/Mac, - (, ):

<html>
    <head>
        <style type="text/css">
        #data h2 {
            /* colour should be same as BG colour, stops element expanding 
                on hover */
            border: 1px solid #fff;
            /* indicates to user that they can do something */
            cursor: pointer;
        }

        #data h2:hover { /* Note this isn't supported in IE */
            border: 1px solid #333;
        }

        .hidden p {
            display:none;
        }
        </style>
        <script type="text/javascript">
        function init() {
            var list = document.getElementById('data');
            var rows = list.getElementsByTagName('li');
            var active;
            for(var i = 0; i < rows.length; i++) {
                var row = rows[i];
                // Hide if not the first, otherwise make active
                if(i != 0) {
                    row.className = "hidden";
                } else {
                    active = row;
                }
                row.getElementsByTagName('h2').item(0).onclick = function() {
                    active.className = "hidden";
                    this.parentNode.className = "";
                    active = this.parentNode;
                }
            }
        }
        </script>
    </head>
    <body onload="init()">
        <!-- I'm using <ul> here since you didn't state the actual markup, 
            but you should be able to adapt it to anything -->
        <ul id="data">
            <li>
                <h2>Row 1</h2>
                <p>Some text here</p>
            </li>
            <li>
                <h2>Row 2</h2>
                <p>Some text here</p>
            </li>
            <li>
                <h2>Row 3</h2>
                <p>Some text here</p>
            </li>
        </ul>
    </body>
</html>

, JavaScript, ( ) .

+2
+1

Source: https://habr.com/ru/post/1707984/


All Articles