2017-04-06 85 views
0

我遇到了展开/折叠菜单的问题。在菜单上,我试图做两件事:首先是展开/折叠所有。这工作得很好。作为参考,我的代码如下:展开/折叠所有人与个人

$("#expandAll dt a").click(function() { 
       $("dd").slideToggle(); 
      }); 

我只是要求它将slidetoggle函数应用于所有dd标记。

我还想在列表中展开一个项目,而其余项目保持折叠状态。但是,当我这样做时,它会扩展整个列表,除了我想要的那个,我正在寻找完全相反的行为。对于单件的代码如下:

function excol() { 
    $("#expand dt a").click(function() { 
     $(this).parent().siblings("dd").slideToggle(); 
    }); 
} 

我的“这个”说法应改为“切换上/下与标签DD是此元素的父的兄弟姐妹一个单一的元素。”问题是,它会切换每一个dd,除了我想要切换的那个。

下面是相关的jQuery代码:

 //Upon successful ajax call 
    success: function (data) { 


     excol(); 

     $("#expandAll dt a").click(function() { 
      $("dd").slideToggle(); 
     }); 
    }, 

}); 

} 

function excol() { 
$("#expand dt a").click(function() { 
    $(this).parent().siblings("dd").slideToggle(); 
}); 
} 

下面是HTML代码:

<div id="expandAll"> 

       <dl> 
        <dt> 
         <a style="font-size: 12px; color: black;"  href="#">Expand/Collapse All</a> 
        </dt> 
       </dl> 

       <div id="expand"> 
        <div id="mhsPrograms"> 
         <div id="MHS"> 
         </div> 
        </div> 
        <br /> 
       </div> 
</div> 

我觉得我很接近解决方案,但有冲突某处,我”米没有看到。任何帮助,将不胜感激。

+0

编辑:两种功能工作时,我分别尝试出来。但是,当我将扩展全部包含在内时,问题就开始了,因此可能会出现我没有看到的冲突。 – Rumble1701a

回答

0

Got it!问题在于展开全部函数的位置。我没有把所有的代码放在这里,因为我认为它没有关系......我现在看到了我的方式的错误,并将发布一切。无论如何,我需要将展开/折叠全部功能放到我的文档就绪功能中。所以我的代码如下所示:

$(document).ready(function() { 
//Call process function 
processListItems(Url, Listname); 
excolAll(); 
//************************* 
//processListItems function 
//************************* 


}); 

exolAll()是问题函数。我仍然不完全理解这些操作,但它现在可行。

完整的代码如下供参考:

$(document).ready(function() { 
//Call process function 
processListItems(Url, Listname); 
excolAll(); 
//************************* 
//processListItems function 
//************************* 


}); 

function processListItems(url, listname, complete, failure) { 
// Executing our items via an ajax request 
$.ajax({ 
    url: url + "/_api/web/lists/getbytitle('" + listname + "')/items? $orderby=SortOrder", 
    method: "GET", 
    headers: { "Accept": "application/json; odata=verbose" }, 
    //Upon successful ajax call 
    success: function (data) { 

     ewiArray(data); 
     excol(); 
     //excolAll(); 
     joinAll(); 

    }, 
}); 
} 

//complete(data);// Returns JSON collection of the results     

//Function for expanding/collapsing menu 
function excol() { 
$("#expand dt a").click(function() { 
    $(this).parent().siblings("dd").slideToggle(); 
}); 
} 

//Function for expanding/collapsing all 
function excolAll() { 
$("#expandAll dt a").click(function() { 
    $("dd").slideToggle(); 
}); 
} 

//Function for joining EWI program to its associated description 
function joinAll() { 
$("button").click(function() { 
    var fired_button = $(this).val(); 
    sessionStorage.setItem('ewiNum', fired_button); 
    window.location.href = "Application.aspx"; 
    return false; 
}); 
}