gpt4 book ai didi

javascript - 在html字符串中查找多个标签

转载 作者:行者123 更新时间:2023-11-28 15:37:58 25 4
gpt4 key购买 nike

var findnumberoftags = "<p>hihih</p><h2></h2><li>one</li><li>two</li><p>iam the best</p><h1>yes</h1><h2></h2>"

我想查找 <h2> 的号码我的字符串中的标签?我怎样才能做到这一点?

OUTPUT: 2

最佳答案

将字符串作为jquery对象,然后可以使用jquery方法来统计标签。

var findnumberoftags ="<p>hihih</p><h2></h2><li>one</li><li>two</li><p>iam the best</p><h1>yes</h1><h2></h2>";
alert($(findnumberoftags ).filter("h2").length)

Fiddle

关于javascript - 在html字符串中查找多个标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24998620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com