简体   繁体   English

使用JavaScript关闭Ajax手风琴

[英]Close ajax accordion with javascript

I have an ajax accordion on that I want to close after 5 seconds from the page loading. 我有一个ajax手风琴,我想在页面加载后5秒后关闭。 I'm trying to do this with javascript but having no luck. 我正在尝试使用javascript做到这一点,但没有运气。 I'm trying to change the accordion control of SelectedIndex to a value that will collapse the accordion. 我正在尝试将SelectedIndex的手风琴控件更改为将折叠手风琴的值。 SelectedIndex = 0 is open (default), and I'm trying to change it to 1 after the javascript timer runs out. SelectedIndex = 0是打开的(默认),并且我试图在javascript计时器用完后将其更改为1。

$(document).ready(function () {
// close the accordion after 5 seconds
setTimeout(function () { document.getElementById('MainContent_Accordion1').SelectedIndex = 1; }, 5000); });

I'm not getting any errors on the java from the console, but the accordion isn't closing. 我从控制台上没有在Java上收到任何错误,但是手风琴没有关闭。 Any thoughts? 有什么想法吗?

I finally found something that works. 我终于找到了可行的方法。

setTimeout(function () {
            var behavior = $get("<%Accordion1.ClientID%>").AccordionBehavior;
            behavior.set_SelectedIndex(1);}, 5000);

This solution does what I wanted, in that it behaves just as if the user clicked the accordion button. 该解决方案可以实现我想要的功能,就像用户单击手风琴按钮一样。 The other solutions I found basically would do an instantaneous hide and not animate the collapse of the accordion. 我发现的其他解决方案基本上是瞬时隐藏,而不是为手风琴的崩溃设置动画。 By setting the index to a value higher than the maximum number, it in effect collapses the accordion. 通过将索引设置为高于最大数值的值,实际上会使折叠手风琴崩溃。 In my case I only have one pane, so setting the index from 0 to 1 will do the trick. 就我而言,我只有一个窗格,因此将索引从0设置为1可以解决问题。

First of all Accordion panel's SelectedIndex property doesn't state that the Accordion panel is open or close. 首先,“ Accordion面板的SelectedIndex属性未声明“手风琴”面板处于打开或关闭状态。 It indicates which pane out of many is currently displayed. 它指示当前显示的窗格数。 So, it could be 0, 1, 2 ... n (where n indicates number of panes - 1). 因此,它可以是0, 1, 2 ... n (其中n表示窗格数-1)。

The definition for the SelectedIndex property from Ajax Control Toolkit website is as follows Ajax Control Toolkit网站上SelectedIndex属性的定义如下

SelectedIndex - The AccordionPane to be initially visible SelectedIndex最初显示的AccordionPane

And, this value is stored in a hidden field with the name containing " _AccordionExtender_ClientState ". 并且,此值存储在名称包含“ _AccordionExtender_ClientState ”的隐藏字段中。

TIP 小费

Ok, back to solving your issue. 好,回到解决您的问题。 First of all you need to think in HTML rather than server side properties of the Accordion control because you are going to manipulate it in client side using JavaScript . 首先,您需要考虑HTML而不是Accordion控件的服务器端属性,因为您将使用JavaScript在客户端对其进行操作。 Accordion control is translated to a bunch of DIV elements in client side with CSS class names like accordionHeader and accordionHeaderSelected (this could vary depending on whether you've assigned custom classes during design time). Accordion控制转换成一堆DIV在客户端与元素CSS类的名称,如accordionHeaderaccordionHeaderSelected (这可能取决于您是否已经在设计时指定自定义类而异)。 See these images and focus on the highlighted areas. 查看这些图像,然后将焦点放在突出显示的区域上。

When the pane is expanded 窗格展开时 在此处输入图片说明

When a pane is expanded it's DIV contains class = "accordionHeaderSelected" . 扩展窗格时,其DIV包含class = "accordionHeaderSelected" And, the hidden filed's value is the index of the pane selected (in this case first pane, ie 0). 并且,隐藏字段的值是所选窗格的索引(在本例中为第一个窗格,即0)。 Also note the style property of the selected pane's content DIV. 还要注意所选窗格的内容DIV的style属性。

When the pane is collapsed 窗格折叠时 在此处输入图片说明

When a pane is collapsed it's DIV contains class = "accordionHeader" . 当窗格折叠时,其DIV包含class = "accordionHeader" And, the hidden filed's value is always -1. 并且,隐藏字段的值始终为-1。 Also note the style property of the selected pane's content DIV. 还要注意所选窗格的内容DIV的style属性。

Therefore, in your JavaScript make use of these values to identify the currently expanded DIV. 因此,在JavaScript ,请利用这些值来标识当前扩展的DIV。 Then do follow these steps. 然后按照以下步骤操作。

  1. change the element class to class = "accordionHeader" 将元素类更改为class = "accordionHeader"

  2. Find the content DIV related to the selected pane and close it by setting following style of the content DIV. 找到与所选窗格相关的内容DIV,然后通过设置以下内容DIV样式将其关闭。

    style = 'height: 0px; style ='height:0px; overflow: hidden; 溢出:隐藏; display: none; 显示:无; opacity: 0;' 不透明度:0;'

  3. You should also make the hidden field value = -1 您还应该使隐藏字段值= -1

Hope this make sense to you. 希望这对您有意义。 Let me know if you need further explanation. 让我知道您是否需要进一步的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM