我正在尝试从类名中提取数字并将其添加到数组中,但我收到错误消息“对象不支持属性或方法'父级'”。 我刚才正在提取类名。
由于
<ul id="catbox">
<li class="cat-item cat-item-2">
<a href="http://oursite/events/category/internal-events/">Internal Events</a>
<ul class='children'>
<li class="cat-item cat-item-8">
<a href="http://oursite/events/category/internal-events/civic-and-ceremonial/">Civic and Ceremonial</a>
</li>
<li class="cat-item cat-item-6">
<a href="http://oursite/events/category/internal-events/economy-and-skills-education-tourism-town-centre-business-events/">Economy and Skills – Education, Tourism, Town Centre, Business Events</a>
</li>
<li class="cat-item cat-item-7">
<a href="http://oursite/events/category/internal-events/health-and-social-care/">Health and Social Care</a>
</li>
<li class="cat-item cat-item-13">
<a href="http://oursite/events/category/internal-events/other-internal-events/">Other</a>
</li>
<li class="cat-item cat-item-5">
<a href="http://oursite/events/category/internal-events/safer-communities-vibrant-communities-leisure-trust/">Safer Communities – Vibrant Communities, Leisure Trust</a>
</li>
</ul>
</li>
</ul>
<script type="text/javascript">
$("#test").load("/events/cal-page/");
$(".cat-item").prepend("<input type=\"checkbox\" > ");
$(":checkbox").change(function () {
if (this.checked) {
var values = $('input:checkbox:checked').map(function () {
return this.parent().attr('class');
}).get();
alert(values);
$("#test").load("/events/cal-page/?q=" + number + "");
}
});
</script>
答案 0 :(得分:0)
.parent()
是jQuery
包裹元素的方法。this
中的callback
代表DOM
元素,而不代表jQuery
包裹的元素。
使用return $(this).parent().attr('class');
$(".cat-item").prepend("<input type=\"checkbox\" >");
$(":checkbox").change(function() {
if (this.checked) {
var values = $('input:checkbox:checked').map(function() {
return $(this).parent().attr('class');
}).get();
alert(values);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<ul id="catbox">
<li class="cat-item cat-item-2"><a href="http://oursite/events/category/internal-events/">Internal Events</a>
<ul class='children'>
<li class="cat-item cat-item-8"><a href="http://oursite/events/category/internal-events/civic-and-ceremonial/">Civic and Ceremonial</a>
</li>
<li class="cat-item cat-item-6"><a href="http://oursite/events/category/internal-events/economy-and-skills-education-tourism-town-centre-business-events/">Economy and Skills – Education, Tourism, Town Centre, Business Events</a>
</li>
<li class="cat-item cat-item-7"><a href="http://oursite/events/category/internal-events/health-and-social-care/">Health and Social Care</a>
</li>
<li class="cat-item cat-item-13"><a href="http://oursite/events/category/internal-events/other-internal-events/">Other</a>
</li>
<li class="cat-item cat-item-5"><a href="http://oursite/events/category/internal-events/safer-communities-vibrant-communities-leisure-trust/">Safer Communities – Vibrant Communities, Leisure Trust</a>
</li>
</ul>
</li>
</ul>
答案 1 :(得分:0)
在$(this)
中使用jQuery对象this
代替map()
,如下所示。
$(this).parent().attr('class');
答案 2 :(得分:0)
你的问题是你没有使用jquery元素来调用parent()方法,而taht来自jQuery。
更改此行,这是$(this)。
runOnUiThread(new Runnable() {
@Override
public void run() {
// change ui
}
});