English 中文(简体)
A. 与 Java本的汇总内容
原标题:Toggling elements with JavaScript

I want to toggle a div element (expand/collapse) when clicked. I have many div elements, on click to new element, I want to collapse the previous one and expand the current clicked one.

I tried using static type variable to save the instance of previous div tag and compared with the current selection, but I don t know why is it not working.

在寻求这一点时,我有着类似的法典想法,以便倒掉所有四舍五入,然后只扩大目前选定的部分,但我想仅仅把前面的一条与新的一环相提并论,不要倒一切,扩大选定的部分(尽管如果不可能有其他方式的话,我会加以利用)。

是否能够利用静 of的静态变量来做到这一点?

最佳回答

At its simplest, you can simply do something like this:

var divs = document.getElementsByTagName( div ),
    collapseClass =  collapsed ,
    current = divs[0];

// Hide all elements except first, add click event hander
for(var i = 0, len = divs.length; i < len; i++){
    divs[i].onclick = function(){
        if(this !== current){
            toggle(this, current);
            current = this;
        }
    };

    if(i > 0) toggle(divs[i]);
}

这将将现有要素储存在一个变数中,然后在另一个要素被点击时加以分析。 它还使用if 说明,以核实目前被点击的元件是否是目前可见的元件,而如果是,则只列.。

See a working demo of this here: http://jsfiddle.net/GaxvM/

问题回答

你们可以给每个要素指定一个独一无二的身份证,并使用文件。 抵达 内容 a 先查明这两个因素,然后倒塌。

如果你按顺序排列(如四分一、四分二、四分三等),你可以做如下事情:

function colexp(div_id){
     div_2_collapse = document.getElementById(div_id);
     next_div = div_id.substr(0,3) + parseInt(div_id.substr(3))+1;
     div_2_expand = document.getElementById(next_div);
     hide(div_2_collapse);
     show(div_2_expand);
}




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签