使用jQuery查找字符串中的元素数 [英] Find number of element in string with jQuery

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

问题描述

我有一个函数,这个函数在参数中接收一个字符串。此变量的内容是HTML代码。我想知道HTML元素的数量与类 .myClass

I have a function, this function receive a string in argument. The content of this variable is HTML code. I'd like know number of HTML element with the class .myClass

function MyTest(myData){
    //get how many element with .myClass are in myData
}

谢谢,

推荐答案

这几乎是jQuery 101。

This is pretty much jQuery 101.

function myTest(myData) {    
  return $( '.myClass', myData ).length;
}

// Usage:
myTest( '<p><span class="myClass">Hello</span> <span class="myClass">world</span></p>' );
// => 2

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

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