计数使用jQuery的HTML字符串元素 [英] Counting HTML string elements using Jquery

查看:101
本文介绍了计数使用jQuery的HTML字符串元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有说取一个HTML字符串,就像一个Ajax请求:

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

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

和我要计数的数字视频当我从服务器获取的HTML 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?

我想:

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

但它返回0。

but it returns 0.

推荐答案

根据返回什么,你必须在根的所有。视频元素。所以有办法让。视频元素的数量是使用 .filter 方式:

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;

这篇关于计数使用jQuery的HTML字符串元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆