最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Counting HTML string elements using Jquery - Stack Overflow

programmeradmin4浏览0评论

I've got an ajax request that fetches an HTML string, like:

<div class="video">...</div><div class="video">...</div> 

and I want to count the number of "video" div's as soon as I retrieve the HTML from the server. Is there an easy way to do this?

I tried:

.done(function(data) { 
        $(data).find('.video').length

but it returns 0.

I've got an ajax request that fetches an HTML string, like:

<div class="video">...</div><div class="video">...</div> 

and I want to count the number of "video" div's as soon as I retrieve the HTML from the server. Is there an easy way to do this?

I tried:

.done(function(data) { 
        $(data).find('.video').length

but it returns 0.

Share Improve this question edited Jun 5, 2014 at 9:09 Mark 2,43511 gold badges31 silver badges50 bronze badges asked Jun 5, 2014 at 9:05 b0xxed1nb0xxed1n 2,3546 gold badges21 silver badges30 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

According to what is returned, you have all .video elements in the root. So one way to get the number of .video elements is to use .filter method:

$(data).filter('.video').length;
发布评论

评论列表(0)

  1. 暂无评论