jQuery - 未捕获的TypeError:Object#< HTMLElement>没有方法'父' [英] jQuery - Uncaught TypeError: Object #<HTMLElement> has no method 'parent'
问题描述
我的代码有问题。 Chrome代码观众说第21行有问题,问题是这样的: Uncaught TypeError:Object#< HTMLElement>没有方法'parent'
。这是jQuery代码:
Something is wrong with my code. Chrome code spectrator say that there is probmel in line number 21, problem like this: Uncaught TypeError: Object #<HTMLElement> has no method 'parent'
. This is jQuery code:
$('.plus').click(function(){
if ($(this).text() == "+"){ /* 1 IF */
$(this).text("-");
if (! ($(this).parent().next().is('ul'))){ /* 2 IF */
var element_id = $(this).parent().attr('id')
var id = '#' + element_id
var url = "/accounts/profile/thingh_update/" + element_id + "/";
$.getJSON(url, function(data){
var items = []
$.each(data, function(index, value) { if(value.pk)
items.push('<li id="' + value.pk + '">' + value.fields.title + ' <span class="plus">+</span></li>');});
$('<ul/>', {
html: items.join('')
}).insertAfter(id); })
} else {(this).parent().next().children().show()} /* 2 ELSE */
} else {$(this).text('+').parent().next().hide()} /* 1 ELSE */
})
})
我的html如下所示:
My html looks like this:
<ul>
<li id="3"><a href="/my/proper/url/">this is for eat</a>
<span class="plus">+</span></li> <!-- after clickin this I get proper json data, and "+" is cahnged to "-", and the next "<ul>" element appear -->
<ul>
<li id="4">fishes
<span class="plus">+</span></li>
</ul>
</ul>
当我点击第一个li元素中的 - 然后再点击+我得到:
And when I click on "-" in first "li" element and then again on "+" i get:
Uncaught TypeError: Object #<HTMLElement> has no method 'parent'
也许我错过了一些明显的东西,但我还没有进入jQuery。我要求对初学者有一些宽容,以及我在哪里可以搞错的任何想法。
谢谢。
Maybe i missed something obvious, but I am not advanced in jQuery yet. I ask for some tolerance for beginner and any sugegestions of where could I do mistake. Thanks.
推荐答案
在这一行:
} else {(this).parent().next().children().show()}
您在(此)
之前缺少 $
,这意味着您试图在DOM元素上调用 parent()
,而不是jQuery对象。
You are missing the $
before (this)
, which means you are trying to call parent()
on a DOM element, rather than a jQuery object.
这篇关于jQuery - 未捕获的TypeError:Object#< HTMLElement>没有方法'父'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!