2014-09-24 3 views
0

HTML :글리 icon을 왜 다시 바꿀 수 없습니까?

<span class="glyphicon glyphicon-star" data-toggle="collapse" data-target="#[email protected](i.tostring)" id="[email protected](i.ToString)" onclick=diffImage(this)></span> 

JAVASCRIPT :

function diffImage(span) { 
    var icon = document.getElementById(span.id); 
    if (icon.getElementsByClassName("glyphicon glyphicon-star")) { 
     icon.className = "glyphicon glyphicon-heart"; 
     } else { 
     icon.className = "glyphicon glyphicon-star"; 
    } 
} 

하나가 다시 스타로 변경 couldn'd 그 후 마음에 스타에서 변경 ... 수 있지만 누릅니다.

내가 클래스 이름은 그래서 같은 방식으로 일치하지 않습니다를 통해 두 번째 다른

Microsoft JScript runtime error: Function expected 
+0

코드를 변경하는 방법을 알고 있습니까 ????? –

답변

0

표시됩니다

if (icon.className("glyphicon glyphicon-star")) { 

로 변경합니다.

function diffImage(span) { 
    var icon = document.getElementById(span.id); 
    var className = document.getElementById(span.id).className; 
    if (className == "glyphicon glyphicon-star") { 
     icon.className = "glyphicon glyphicon-heart"; 
    } else { 
     icon.className = "glyphicon glyphicon-star"; 
    } 
} 
+0

고맙습니다 !!!!!!!!!!!!!!!!! –

+1

'span'을 직접 사용할 수도 있습니다. 'document.getElementById (span.id)'필요 없음. – Jasen