2012-09-03 5 views
1

링크를 클릭 할 때 이미지를 DIV에로드하는 jQuery의 공개 기능이 있습니다.jQuery reveal - 기본 이미지

$(function(){ 
jQuery(".gallery-item a").click(function(evt) { 
    evt.preventDefault(); 
    jQuery("#imageBox").empty().append(
     jQuery("<img>", { src: this.href}) 
    ); 
}); 
}); 

페이지가로드 될 때 이미 사업부의 이미지가 있도록 기본 이미지를 추가 할 수있는 방법이 있습니까?

+0

은 그냥 HTML에 그것을 추가 할 수 없습니다 방법을 볼? – MarcoK

+0

페이지로드시 첫 번째 ''을 클릭하기 만하면됩니다. – adeneo

답변

1
$(function() { 
    $(".gallery-item a").click(function (evt) { 
     evt.preventDefault(); 
     var box = $("#imageBox").html('<img src="default.jpg" />'); 

     $("<img>", {src: this.href}).load(function() { 
      box.empty().append(this); 
     }); 
    }); 
}); 
+0

링크 중 하나를 클릭하기 전까지이 항목에서 아무것도 얻지 못합니다. 그러면 기본 이미지가 클릭 한 것과 교체되기 전에 잠시 표시됩니다. – fightstarr20

0

이 시도 :

이미지의 URL은 "데이터 urlimage"태그 "는"

가 속성에 있다고 가정하면 코멘트

$(function() { 
    jQuery(".gallery-item a").click(function(evt) { 
     jQuery("#imageBox").empty().append(
      jQuery("<img />", { src: $(this).attr("href") }) 
     ); 
     return false; 
    }); 
});​ 

편집

Html :

<div class="gallery-item"> 
<a data-urlimage="/img/myimage.jpg" href="#">click here</a> 
</div> 

자바 스크립트 :

$(function() { 
    jQuery(".gallery-item a").click(function(evt) { 

     var img = new Image(); 
     img.src = $(this).data("urlimage"); 
     img.onload = function(){ 
      jQuery("#imageBox").empty().append(img); 
     }; 

     return false; 
    }); 
});​ 

see image events

편집 : 더 나은 개체 "이미지"를 만들

+0

이 방법을 사용하면 이미지 URL을 어디에 두겠습니까? 내가 href를 대체하겠습니까? – fightstarr20

+0

"a"태그의 "href"속성에 이미지의 URL이 있다고 가정했습니다. 원하는대로 "href"를 대체 할 수 있다면 –

관련 문제