Jquery - 如果单击父类隐藏/显示,则切换图像Src

时间:2014-05-15 15:27:42

标签: javascript jquery html css

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

$(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);
});
});

要复制:切换打开所有父级和子级,然后关闭其中一个父级,然后重新打开父级。您会注意到+/-图标仍处于其之前的状态

jsFiddle link

1 个答案:

答案 0 :(得分:0)

您可以在当前的子类别下找到子类别的img个节点,然后更改其src attr。

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

代码可以是:

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