find不是函数jquery

美好的一天,

我有我的观点,我在尝试渲染模板

Editar

data[i].Descripcion

和ajax电话

 //ajax starts here... //.... success: function (data) { $.each(data, function (index, item) { var clone = $('#template').clone().html(); console.log(clone); parent.append(clone); clone.find('.editar').attr('href', editarUrl + '/' + item.Id); clone.find('.centrar').text(item.Titulo); clone.find('.index').attr('href', indexUrl + '?CursoId' + item.Id); clone.find('.image').attr('src', item.ImagenUrl); clone.find('.description').text(item.Descripcion); }) 

我使用控制台日志来查看变量克隆是否包含html,它确实存在。我的问题是我收到一条消息>>> find is not a function。

我该怎么办? 谢谢。

问题是因为clone是一个包含#template元素的HTML的字符串。 它不是一个jQuery对象。 这就是你得到错误的原因。

要解决此问题,您需要在jQuery对象中引用克隆的内容。 您可以通过使用appendTo()并存储引用来实现。 像这样的东西:

 success: function (data) { $.each(data, function (index, item) { var html = $('#template').html(); var $clone = $(html).appendTo(parent); $clone.find('.editar').attr('href', editarUrl + '/' + item.Id); $clone.find('.centrar').text(item.Titulo); $clone.find('.index').attr('href', indexUrl + '?CursoId' + item.Id); $clone.find('.image').attr('src', item.ImagenUrl); $clone.find('.description').text(item.Descripcion); }); });