2017-02-10 53 views
-1

创建我自己的照片库我想将小图片加载到更大的div来放大它。首先,我使用html(),但它删除主要(小)照片,或什么都不做(我发现了几个解决方案如何使用html()而不需要对主数据进行解析,但是没有tchem工作)。所以我尝试使用clone()。但后来我尝试将它与html()结合起来,它给我错误:this.clone不是一个函数。我再次尝试找到解决方案,但没有任何工作。 这里是我的代码与我tryed使用的解决方案和效果:jquery clone()with html()不是函数

<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> 

请帮我解释一下我做错了什么......

+1

你有几个地方使用'clone()'和'html()'。 j08691回答了你为什么收到错误,但我必须说,你的代码很混乱。你不应该用'this'调用'html()'; ['html()'](http://api.jquery.com/html)是为了和HTML一起作为字符串(或函数,而不是HTML元素或jQuery对象)调用的。 –

+0

我只在这里的几个地方使用clone()和html()来显示我尝试使用的解决方案,但它们不起作用。那么,我应该在我的代码中使用od html()吗? – FKa

+0

取决于你想要做什么。如果您想用元素替换“#picture”的内容,请使用['empty()'](http://api.jquery.com/empty/),后跟['append'](http:// api.jquery.com/append/)将是你最好的选择。但是,真的,你应该看看关于jQuery的一些教程,也许阅读文档。 –

回答

6

this.clone().clone()是一个jQuery的功能,但你”试图在this上使用它,这是纯粹的JavaScript。

尝试用$(this).clone()代替。

+0

它的工作! :-) 谢谢 – FKa