简体   繁体   中英

How do I open javascript div tabs with a link on the page

I am trying to use a link to open a specific tab that is created using divs and the tabs open and close using javascript.

Here are the tabs and the javascript is within the script tags at the bottom:

<div class="span12 module_cont module_tabs">      
        <div class="shortcode_tabs">
            <div class="all_heads_cont">
                <div class="shortcode_tab_item_title it1 head1 active" data-open="body1">%%LNG_ProductDescription%%</div>
                <div class="shortcode_tab_item_title it2 head2" id="reviews" data-open="body2">%%LNG_JS_Reviews%%</div>
                <div class="shortcode_tab_item_title it3 head3" data-open="body3">%%LNG_JS_ProductVideos%%</div>
                <div class="shortcode_tab_item_title it4 head4" data-open="body4">%%LNG_JS_SimilarProducts%%</div>
            </div>
            <div class="all_body_cont">
                <div class="shortcode_tab_item_body body1 it1">
                    <div class="ip">
                        %%Panel.ProductDescription%%
                    </div>
                </div>
                <div class="shortcode_tab_item_body body2 it2">
                    <div class="ip">
                        %%Panel.ProductReviews%%                               
                    </div>
                </div>
                <div class="shortcode_tab_item_body body3 it3">
                    <div class="ip">
                        %%Panel.ProductVideos%%                                           
                    </div>
                </div>
                <div class="shortcode_tab_item_body body4 it4">
                    <div class="ip">
                        %%Panel.SimilarProductsByTag%%
                        %%Panel.ProductByCategory%%
                        %%Panel.ProductVendorsOtherProducts%%
                        %%Panel.SimilarProductsByCustomerViews%%               
                    </div>
                </div>
            </div>
        </div><!-- .shortcode_tabs -->                             
        <script type="text/javascript">
            jQuery('.shortcode_tabs').each(function(index) {                                    
                /* GET ALL HEADERS */
                var i = 1;
                jQuery('.shortcode_tab_item_title').each(function(index) {
                    jQuery(this).addClass('it'+i); jQuery(this).attr('whatopen', 'body'+i);
                    jQuery(this).addClass('head'+i);
                    jQuery(this).parents('.shortcode_tabs').find('.all_heads_cont').append(this);
                    i++;
                });
                /* GET ALL BODY */
                var i = 1;
                jQuery('.shortcode_tab_item_body').each(function(index) {
                    jQuery(this).addClass('body'+i);
                    jQuery(this).addClass('it'+i);
                    jQuery(this).parents('.shortcode_tabs').find('.all_body_cont').append(this);
                    i++;
                });
            });
            jQuery('.shortcode_tabs .all_body_cont div:first-child').addClass('active');
            jQuery('.shortcode_tabs .all_heads_cont div:first-child').addClass('active');

            jQuery('.shortcode_tab_item_title').live('click', function(){
                jQuery(this).parents('.shortcode_tabs').find('.shortcode_tab_item_body').removeClass('active');
                jQuery(this).parents('.shortcode_tabs').find('.shortcode_tab_item_title').removeClass('active');
                var whatopen = jQuery(this).attr('data-open');
                jQuery(this).parents('.shortcode_tabs').find('.'+whatopen).addClass('active');
                jQuery(this).addClass('active');
            });

            jQuery('reviews').live('click', function(){
                jQuery(this).parents('.shortcode_tabs').find('.shortcode_tab_item_body').removeClass('active');
                jQuery(this).parents('.shortcode_tabs').find('.shortcode_tab_item_title').removeClass('active');
                var whatopen = jQuery(this).attr('data-open');
                jQuery(this).parents('.shortcode_tabs').find('.'+whatopen).addClass('active');
                jQuery(this).addClass('active');
            });                                               
        </script>                                        
</div><!-- .module_cont -->

All I want to do is have a link on the same page that activates the reviews tab (id="reviews") and is also known as the body2.

all I have so far for my link is:

<a href="#reviews">Reviews</a>

Help. My brain hurts...

You just need to set the link up so that it doesn't take the user to a new page, and then setup a click event to open the tab up. Example:

HTML:

<a href='javascript:void(0);' id='reviewLink'>Review Tab</a>

JavaScript:

$('#reviewLink').click(function() {
    $('.shortcode_tab_item_body').css({display:'none'});
    $('.body2').css({display:'none'});
});

Or a jsfiddle here: http://jsfiddle.net/tKLhn/

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