【问题标题】:jquery clone() with html() is not a function带有 html() 的 jquery clone() 不是函数
【发布时间】: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() 呢?
  • 取决于你想做什么。如果你想用元素替换“#picture”的内容,empty() 后跟append 是你最好的选择。但实际上,你应该看看一些关于 jQuery 的教程,或者阅读文档。
  • 我想将点击的 img 对象加载到 div#picture。所以我认为 html() 可以吗?
  • 显然这是您的选择。如果我看到我的一个程序员将​​不是字符串或函数的东西传递给html(),我会让他们改变它。仅仅因为它现在可以工作,并不意味着它可以在更高版本的 jQuery 中工作。

标签: jquery html clone


【解决方案1】:

this.clone() 中,.clone() 是一个 jQuery 函数,但您正尝试在纯 JavaScript 的 this 上使用它。

改用$(this).clone()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    相关资源
    最近更新 更多