javascript - Counting HTML string elements using Jquery - Stack Overflow

I've got an ajax request that fetches an HTML string, like: <div class="video">...

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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744245813a4564921.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信