简体   繁体   English

尝试追加元素后,jQuery停止执行

[英]jquery stops execution after trying to append element

My function is supposed to copy insides of element twice, and append them, but for some reason jQuery stops executing after first append. 我的函数应该复制两次元素内部,然后追加它们,但是由于某种原因,jQuery在第一次追加之后停止执行。 I do not understand why. 我不理解为什么。

$(".testowyUl").prepend(function () {
  var $temp = $(this).children().clone();
  var $temp2 = document.createElement("div");
  $temp2.append($temp);
  $temp2.append($(this).children().clone());
  return $temp2.children();
});

Try: 尝试:

var $temp2 = $("<div>");

instead of: 代替:

var $temp2 = document.createElement("div");

to create a jquery object. 创建一个jQuery对象。 As there is no method "append" in DOM element. 由于DOM元素中没有“附加”方法。

you are calling append on a non Jquery object at 您在非Jquery对象上调用append

$temp2.append($temp);

resulting in 导致

Object #<HTMLDivElement> has no method 'append'

update: 更新:

you could try by using 你可以尝试使用

var $temp2 = $("div");

instead of 代替

var $temp2 = document.createElement("div");

see a working fiddle here 在这里看到一个工作的小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM