查找不是函数jQuery [英] find is not a function jquery
问题描述
美好的一天
我有看法,我正在尝试渲染模板
I have my view, where I am trying to render a template
<div id="template" style="display:none;">
<div class="col-lg-4 col-md-6 col-sm-12">
<div class="thumbnail">
<a href="" class="btn btn-primary btn-sm editar">Editar</a>
<h3 class="centrar"></h3>
<a class="index" href="">
<img class="image" src="" />
</a>
<p class="description">data[i].Descripcion</p>
</div>
</div>
和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);
})
我使用控制台日志来查看变量clone是否包含html,并且确实包含html.我的问题是我收到了一条消息>>> find不是一个函数.
I used console log to see if the variable clone contains html, and it does.My problem is that I receive a message >>> find is not a function.
我该怎么办?谢谢.
推荐答案
问题是因为clone
是包含#template
元素的HTML的字符串.它不是jQuery对象.这就是为什么您会收到错误消息.
The issue is because clone
is a string that contains the HTML of the #template
element. It's not a jQuery object. This is why you get the error.
要解决此问题,您需要引用jQuery对象中的克隆内容.您可以通过使用appendTo()
并存储引用来实现.像这样:
To fix this, you need to reference the cloned content within a jQuery object. You can achieve that by using appendTo()
and storing the reference. Something like this:
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);
});
});
这篇关于查找不是函数jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!