简体   繁体   中英

jQuery accomplish hide and show functionalities

I'm new to jQuery , I'd like to use it to implement following two features:

  • Hide Message 1 and Message 2 when the user selects Hide option from the dropdown list.

  • When the user selects show, animates hidden text to show slowly and display a message inside div element after the show effect is completed.

This is my html markup example that I need to use later

<select id="operation">
<option value="">Select one...</option>
<option value="1">Show</option>
<option value="2">Hide</option>
</select>

<ul id="li">
<li id="x1" class="myClass1">Message 1</li>
<li id="x2" class="myClass1">Message 2</li>
<li id="x3" class="myClass2">Message 3</li>
</ul>

<div id="msg"></div>

Any help would be appreciated. Thanks

$("#operation").change(function(){

    if ($(this).val() == "2"){ // user chose Hide

        $("#x1, #x2").hide();

    } else if ($(this).val() == "1"){ // user chose Show

        $("#x1, #x2").fadeIn('slow', function(){

            //What to do when fadeIn animation is complete
            $("#msg").html("Hi there!");

        });

    }

});

Untested, but it should work.

Well, I can give you the most easiest answer for jQuery:

    <script>
    $('#operation').on('change', function() {
      if ( this.value == '2');
      {
        $("#x2").hide();
        $("#x3").hide();

      }
      else
      {
        $("#x2").show();
        $("#x3").show();
      }
    });
    });
    </script>

<ul id="li">
<li id="x1" name="test1" class="myClass1">Message 1</li>
<li id="x2" name="test2" class="myClass1">Message 2</li>
<li id="x3" name="test3" class="myClass2">Message 3</li>
</ul>

<select id="operation">
<option value="">Select one...</option>
<option value="1">Show</option>
<option value="2">Hide</option>
</select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM