简体   繁体   中英

JQuery to show a div with same class

I made a simple jQuery to show a div with same class when clicking a link by referring to following code at http://jsfiddle.net/6GvE6/6/ . (You can see the code modified by me at http://jsfiddle.net/UpX3L/1717/ )

$('[id^="wrapper"]').on('click', function(e) {
    e.preventDefault();
    $('.wrap > div').hide();

    $('.'+ this.id).show();
});

Both seem to work in JSFiddle, but when I tried to use them in my WordPress blog, they did not work.

Yes inspect your code. Check in console, if it is '$ is undefined' error.

Replace '$' with 'jQuery' in your code.

This is the standard for WordPress

Here is the final version:

$('[id^="wrapper"]').on('click', function(e) {
    e.preventDefault();
    var id = this.id;
    $('.wrap > div').each(function() {
        if(!$(this).hasClass(id)) {
            $(this).hide();
        }
    });

    $('.'+ this.id).slideToggle('slow');
});

demo

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