[img src] to [background image] 변경 질문드립니다
본문
<div class="imgto">
<ul>
<li>
<div class="image"><img src="이미지.jpg" alt="이미지내용"></div>
</li>
</ul>
</div>
img src 의 내용을 .image 의 background-image 로 변환하려 합니다.
$(function() {
$(".image img").each(function(i, elem) {
var imgLoad = $(elem);
var divLoad = $(".image").css({
background: "url(" + imgLoad.attr("src") + ") no-repeat",
width: imgLoad.width() + "px",
height: imgLoad.height() + "px"
});
divLoad.html(img.attr("alt"));
divLoad.addClass("replacedImage");
imgLoad.replaceWith(divLoad);
});
});
어디서 잘못된건지 실행되지 않아 질문드립니다.
!-->!-->답변 1
$(function(){
$(".image img").each(function(){
var $src = $(this).attr("src");
var $alt = $(this).attr("alt");
var $target = $(this).parent("div");
$target.css("background","url("+$src+") no-repeat center");
$target.text($alt);
$target.addClass("replacedImage");
});
});
이런식으로 작업하면 되지 않을까요?
!-->
답변을 작성하시기 전에 로그인 해주세요.