show/hide div based on select option jquery

后端 未结 4 1547
说谎
说谎 2020-11-27 13:08

Here is my code. Why it doesn\'t work?



        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 13:52

    You're running the code before the DOM is loaded.

    Try this:

    Live example:

    http://jsfiddle.net/FvMYz/

    $(function() {    // Makes sure the code contained doesn't run until
                      //     all the DOM elements have loaded
    
        $('#colorselector').change(function(){
            $('.colors').hide();
            $('#' + $(this).val()).show();
        });
    
    });
    

提交回复
热议问题