2013-03-27 1 views
-1

프로그램을 만들었는데, 이는 표시/숨기기 버튼은 클릭 한 항목에 달려 있습니다. 그것은 간단하지만 큰 문제가 있습니다. 이 버튼을 계층 적으로 배치 할 필요가 없습니다. 처음으로 "
"태그를 추가하려고 시도했지만, .after()를 사용하면 요소를 삭제할 수 없습니다. 왜?jquery switch case and after (anything) stuck

<button id="vissza">Vissza</button><hr /> <!-- vissza means: BACK --> 
<button id="1">First</button> 
<button id="11">AA</button> 
<button id="111">AA1</button> 
<button id="112">AA2</button> 
<button id="113">AA3</button> 
<button id="114">AA4</button> 
<button id="12">AB</button> 
<button id="121">AB1</button> 
<button id="122">AB2</button> 
<button id="123">AB3</button> 
<button id="124">AB4</button> 
<button id="125">AB5</button> 

<button id="2">Second</button> 
<button id="21">BA</button> 
<button id="211">BA1</button> 
<button id="212">BA2</button> 
<button id="213">BA3</button> 
<button id="214">BA4</button> 
<button id="22">BB</button> 
<button id="221">BB1</button> 
<button id="222">BB2</button> 
<button id="223">BB</button> 

<button id="3">Third</button> 
<button id="31">CA</button> 
<button id="32">CB</button> 
<button id="33">CC</button> 
<button id="34">CD</button> 

<script> 

$("button[id]").hide(); 
$("button[id='1']").show(); 
$("button[id='2']").show(); 
$("button[id='3']").show(); 

$("button").click(function (event) { 
event.preventDefault(); 
var CurrentButtonID = $(this).attr('id'); 

LengthOfAButton = CurrentButtonID.length; 
j = 1; 

var ItIsExist = 1; 

switch (LengthOfAButton) { 
case 1: 
$("button[id]").hide(); 
BeforeStep = CurrentButtonID; 

while (ItIsExist>0) { 
ShowThis = "button[id=" + CurrentButtonID + j + "]"; 
$(ShowThis).show().after('[can not dissappear1]'); 
ItIsExist = $(ShowThis).length; 
j++; 
} 

break; 

case 2: 
$("button[id]").hide(); 
BeforeStep = CurrentButtonID; 

while (ItIsExist>0) { 
ShowThis = "button[id=" + CurrentButtonID + j + "]"; 
$(ShowThis).show().after('[can not dissappear2]'); 
ItIsExist = $(ShowThis).length; 
j++; 
} 

break; 

case 3: 
$("button[id]").hide(); 
BeforeStep = CurrentButtonID; 

while (ItIsExist>0) { 
ShowThis = "button[id=" + CurrentButtonID + j + "]"; 
$(ShowThis).show().after('[can not dissappear3]'); 
ItIsExist = $(ShowThis).length; 
j++; 
} 
break; 

case 6: 

if (BeforeStep.length==1) { 
$("button[id]").hide(); 
CurrentButtonID = BeforeStep.substr(0,0); 
} 
if (BeforeStep.length==2) { 
$("button[id]").hide(); 
CurrentButtonID = BeforeStep.substr(0,1); 
} 
if (BeforeStep.length==3) { 
CurrentButtonID = BeforeStep.substr(0,2); 
$("button[id]").hide(); 
} 
if ((BeforeStep.length>1) && (BeforeStep.length<6)) { 
$("button[id]").hide(); 
} 

while (ItIsExist>0) { 
var ShowThis = "button[id=" + CurrentButtonID + j + "]"; 
$(ShowThis).show().after('[can not dissappear /at backbutton/]'); 
ItIsExist = $(ShowThis).length; 
j++; 
} 
BeforeStep = CurrentButtonID; 
break; 
} 

$("button[id='vissza']").show(); 

}); 


</script> 

답변

0

var btns = $("button[id]").hide(); 
$("#1, #2, #3").show(); 

btns.click(function(e){ 
    var $this = $(this); 
    var regex = new RegExp('^' + $this.attr('id') + '\\d$'); 
    btns.filter(function(i, v){ 
     return regex.test(this.id); 
    }).show(); 
}); 

데모를보십시오 : Fiddle

+0

많이 감사, 그것은 나보다 더 간단하다! 하지만 어떻게 단추를 포맷 할 수 있습니까? .after에 "after "를 넣으면.) show();" - >> "}). show(). after ('
');" 그것은 다시 엉망이되고있다. 어떻게 ID-s로 단추를 포맷 할 수 있습니까? – user2215173

+0

구조와 같은 나무를 원하십니까 –

+0

예. 어쨌든, 나는 아이디로, 또는 테이블에, 또는 무엇이든간에 위치를 지정하려고합니다. – user2215173