2011-03-24 5 views
-1

이 모든 것이 정말 이상하게 보일 수 있습니다. var를 .html ('g'+ index)로 참조하려고 할 때 객체 예상 오류가 발생합니다. 나는 var를 만드는 것이 최선이 아니라고 생각하니?.html을 사용할 때 이상한 객체 예상 오류

var g0 = "<?php getFiles("gallery/Audience"); ?>"; 
var g1 = "<?php getFiles("gallery/Exhibition"); ?>"; 
var g2 = "<?php getFiles("gallery/registration"); ?>"; 
var g3 = "<?php getFiles("gallery/Speakers"); ?>"; 

$(".galleryButton").each(function (index) { 
    $(this).click(function(){ 
     $('#galleria').html();        
     $('#galleria').html('g'+index); 
     initiateGallery(); 
    }).mouseover(function() { 
     $(this).css('cursor', 'pointer'); 
     $(this).css('backgroundColor', '#002E53'); 
    }).mouseout(function(){ 
     $(this).css('backgroundColor', '#000'); 
    }); 
}); 
+0

'$ ('# galleria') 줄로 무엇을하려하고 있습니까? html();'? –

답변

1

당신은 변수 GX의 값을 얻기 위해 평가 사용할 수 있습니다.

$(this).click(function(){ 
    $('#galleria').html();        
    $('#galleria').html(eval('g'+index)); 
    initiateGallery(); 
}) 

그러나 평가가 악한대로 변수 얻을 수있는 스위치 케이스를 사용해야합니다

$(this).click(function(){ 
    $('#galleria').html(); 
    var contents; 
    switch(index) { 
     case 1: 
      contents = g1; 
      break; 
     case 2: 
      contents = g2; 
     ... 
    } 
    $('#galleria').html(contents); 
    initiateGallery(); 
}) 

난 당신이 라인 #galleria의 HTML을 취소하려는 추측 :

$('#galleria').html(''); 
:
$('#galleria').html(); 

당신은 취소) (HTML로 내용을 빈 문자열을 통과해야

+0

나는 당신의 솔루션에 동의하지만, 나는'eval'이 모든 경우에 악마라고 생각하지 않는다. 그것은 현명하게 사용되어야한다. –

+0

나는 eval이 최선의 대안 인 상황에 동의한다. 그러나 내가 보는 eval의 대부분의 사용은 eval이 현명하게 사용되는 경우가 아닙니다. – Oscar

+0

Ahhhh yes는 html을 지우려고했지만 ('') thats가 더 잘 작동하는 것을 그리워합니다 ... – MarkBeharrell

0

당신이 그때는 아마 일을 제안 HTML에 텍스트 문자열 'g'+index를 삽입하고자하는 경우 ...

$('#galleria').html('<span>g'+index+'</span>'); 
+0

# 갤러리아가 뭔지도 모릅니다. 이미 스팬 일 수도 있습니다. –

0

eval 방법을 사용해야합니다 : $ ('# galleria') .html (eval ('g'+ index));