jQuery-如果单击父类,则切换图像Src隐藏/显示 [英] Jquery - Toggle Image Src if Parent Class is Clicked Hide/Show

查看:84
本文介绍了jQuery-如果单击父类,则切换图像Src隐藏/显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个配置为单击+/-图标时隐藏/显示表格行的程序.该功能正在运行,但是,当父类别切换为关闭状态时,我需要找出一种重置所有隐藏/显示图标的方法.

I have a program that is configured to hide/show table rows when a +/- icon is clicked. The functionality is working, however, I need to figure out a way to reset all hide/show icons when the parent category is toggled closed.

$(function(){
//src vars
var hide_src = "http://www.synchronizeddesigns.com/filter_hide.gif",
    reveal_src = "http://www.synchronizeddesigns.com/filter_reveal.gif",
        s = '';
//hide all sublevel elements
$(".subsub, .subsubsub").hide();

$("a").click(function(e){
    e.preventDefault();
    var tID = e.target.id,
        tClass = '.' + tID.replace('HS', '');

    $(tClass).toggle();

    if(!$(tClass).is(':visible')){
        s = hide_src;

        //for each subcategory
        $(tClass).each(function(){
            //get class names into classes array
            var classes = $(this).attr('class').split(' '),
                parentClass = '';

            //search classes array for class that begins with 'cat'
            for (var j=0; j<classes.length; j++) {
                if (classes[j].match("cat")){ 
                    parentClass = classes[j];
                }
            }

            //find subsubsub elements that have a class that begins with 'cat#sub'
            var subs = $('[class*=' + parentClass + 'sub]');

            //if there are sub elements, hide them too
            if(subs){
                subs.hide();

                /*****************************************************
                NEED HELP HERE !!!!!!!!!!
                Need a way to reset all hide/show images icon
                when 'parentClass' hide/show is clicked to close.
                *****************************************************/

            }
        });
    } else {
        s = reveal_src;
    }
    //Change image src
    $("#" + tID).attr('src', s);
});
});

要复制:切换打开所有父项和子项,然后关闭其中一个父项,然后重新打开父项.您会注意到+/-图标保持其以前的状态

To replicate: Toggle open all parents and subs, then close one of the parents, then reopen the parent. You'll notice that the +/- icon remains in it's previous state

jsFiddle链接

推荐答案

您可以在当前子类别下找到子类别的img节点,然后更改其src属性.

You can find the img nodes for the subcategories under the current one and then change their src attr.

我已经更新了您的jsfiddle: http://jsfiddle.net/nTyWv/12/

I've updated your jsfiddle: http://jsfiddle.net/nTyWv/12/

代码可能类似于:

            if(subs){
                innerIcons = jQuery.find("a > img[id*="+tID+"sub]");
                if (innerIcons) {
                    $(innerIcons).attr('src', s);
                };
                subs.hide();
            }

这篇关于jQuery-如果单击父类,则切换图像Src隐藏/显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆