2013-10-29 2 views
1

FAQ에 자바 스크립트 드롭 다운을 사용하고 있는데 클릭 할 때 질문의 색상을 변경 한 다음 다시 클릭 할 때 변경하는 방법이 있습니다.FAQ 드롭 다운 - 클릭했을 때 텍스트 색상 변경

<script type="text/javascript"> 
function toggle(Info) { 
var CState = document.getElementById(Info); 
CState.style.display = (CState.style.display != 'block') 
        ? 'block' : 'none';} 
</script> 

내가 사용 알고 :

가 여기에 자바 스크립트의 조치가 질문을 클릭하면만을위한 작동하지만 나는 각각의 색을 켜거나 끕니다 중 하나를 클릭 있도록 스타일을 시도하고, 응답이 떨어지면 그 일이 일어날 것이고 나는 두 가지 모두를 조정하고 싶습니다.

+0

그리고 어떤 HTML 코드 이 행동은? '정보'란 무엇입니까? –

+0

어떻게이 토글을 실행하고 있습니까? [JSFiddle] (http://jsfiddle.net/)을 만들 수 있습니까? – Moob

+0

클릭 한 요소에 클래스를 추가하십시오. – epascarello

답변

1

올바르게 이해하면 토글 기능이 답변을 표시하거나 숨 깁니다.

document.getElementById(your question).classList.toggle(your-class); 

및 CSS 파일

.your-class { 
    color: selected color; 
} 
+0

'classList'는 IE9에서는 유용하지 않습니다. 완전한 지원을 위해 jQuery 또는 shim을 사용하십시오. – David

+0

JQuery와 잘 어울리지 않습니다.이 항목을 추가해야하는 위치와 색상을 설정하는 방법을 명확히 할 수 있습니까? –

+0

jquery에는 정확히 this를 수행하는 .toggleClass() 메서드가 있습니다. jquery를 페이지에 추가해야하고 document.getElementById를 수행하는 대신 $ ('# your question'). toggleClass ('your class')를 수행해야합니다. 그리고 CSS 클래스의 색상을 설정합니다. .your-class {color : 선택한 색상;} – andrei

0
<style> 
.classStyle1 {background-color:white} 
.classStyle2 {background-color:green} 
</style> 
<script type="text/javascript"> 
function toggle(Info) { 
var CState = document.getElementById(Info); 

if(CStage.className == "classStyle1"){ 
    CStage.className = classStyle2; 
}else{ 
    CStage.className = classStyle1; 
} 
// or else 
// create style attribute for select element and put style='background-color:white' like this 
if(CStage.style.backgroundColor == "white"){ 
    CStage.style.backgroundColor = 'green'; 
}else{ 
    CStage.style.backgroundColor = 'white'; 
} 
</script> 
0
에 : 그럼 당신이해야 할 질문 용기를 얻을 예를 들어 텍스트 색상

포함 된 CSS 클래스를 전환하는 것입니다

정확하게 이해하면 시도해주세요. CState=document.getElementById("myColor"); CState.onmouseover=function(){this.style.color='red';}; CState.onmouseout=function(){this.style.color='blue';};

관련 문제