2017-02-10 52 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