【发布时间】:2017-06-29 04:12:45
【问题描述】:
创建我自己的照片库我想将小图片加载到更大的 div 以放大它。首先,我使用 html(),但它删除了主要(小)照片,或者什么也不做(我发现了几个解决方案如何在不删除主要数据的情况下使用 html(),但不是 tchem 工作)。所以我尝试使用克隆()。但后来我尝试将它与 html() 结合起来,它给了我错误:this.clone 不是一个函数。我再次尝试找到解决方案,但没有任何效果。 这是我尝试使用的解决方案和效果的代码:
<script>
var GallObj={
img:<?php echo json_encode($GetImg->jsonData); ?>,
imgIndex:new Number, //storage data about index of enlaging picture
}
var showAllImg = $.map(GallObj.img, function(val, i) {
return "<img src='gallery/"+val+"' class='smallimg'>";
});
$("#gallCont").html(showAllImg.join(""));
$('.smallimg').click(function(){
GallObj.imgIndex=$('.smallimg').index(this);
//it work, but I need change it and use html() because it doesn't change one loaded photo to new loaded photo of course:
$(this).clone().appendTo($('#picture'));
// this two removed clicked oryginalny picture and I don't want it:
$('#picture').html(this)
$('#picture').html(this).html();
//this.clone is not a function error:
elm=this.clone();
$('#picture').html(elm);
//this.clone is not a function error:
$('#picture').html(this.clone());
//do nothing:
$('#picture').clone().html(this);
//do nothing:
var value=$(this).html();
$("#picture").html(value);
$('#mask, #fixed, #picture, #close').css('display','block');
$('#mask').animate({'width':'100%','height':screen.height +100, opacity:'0.5'});
$('#picture').animate({width:'900'});
$('html, body').animate({scrollTop:0}, 1500);
})
</script>
请帮助我并解释我做错了什么......
【问题讨论】:
-
您在多个地方使用
clone()和html()。 j08691 已经回答了您收到错误的原因,但我必须说,您的代码非常混乱。你不应该用this打电话给html();html()是用 HTML 作为字符串(或函数,但不是 HTML 元素或 jQuery 对象)调用的。 -
我仅在此处的几个地方使用了 clone() 和 html() 来展示我尝试使用但它们不起作用的解决方案。那么我应该在我的代码中使用什么来代替 od html() 呢?
-
我想将点击的 img 对象加载到 div#picture。所以我认为 html() 可以吗?
-
显然这是您的选择。如果我看到我的一个程序员将不是字符串或函数的东西传递给
html(),我会让他们改变它。仅仅因为它现在可以工作,并不意味着它可以在更高版本的 jQuery 中工作。