Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 计算多个div中的hashtag数_Javascript_Jquery - Fatal编程技术网

Javascript 计算多个div中的hashtag数

Javascript 计算多个div中的hashtag数,javascript,jquery,Javascript,Jquery,我的页面上有多个。它们没有类或ID。 我试图实现的是能够针对每一个计算其中有多少个hashtag,并在中显示结果 结构示例: <div class="parent"> <p> #test #test #test </p> <span></span> </div> <div class="parent"> <p> #test #test

我的页面上有多个
。它们没有类或ID。 我试图实现的是能够针对每一个
计算其中有多少个hashtag,并在
中显示结果

结构示例:

<div class="parent">
    <p>
        #test #test #test
    </p>
    <span></span>
</div>

<div class="parent">
    <p>
        #test #test #test
    </p>
    <span></span>
</div>

<div class="parent">
    <p>
        #test #test #test
    </p>
    <span></span>
</div>

您使用了错误的选择器,请将其替换为:

  var text = $(this).find("p").text();
还请注意,在您发布的代码中没有
输出
div。
检查此项以获得可用版本。

您可以尝试以下代码:

//使用类parent遍历每个元素
$(“.parent”)。每个(函数(索引){
//获取每英寸的标签数
var tags=$(this.find(“p”).text().split(“#”).length-1;
//在中写入标记的数量
$(this.find(“span”).text(标记);
});


#测试#测试#测试

#测试#测试#测试

#测试#测试#测试


我根本看不到您的代码是如何工作的,因为
将引用
窗口
,并且您正在查找HTML示例中不存在的
元素…?哪个元素的ID为
输出
?HTML中有三种不同的
span
s,它们都没有该ID。
  var text = $(this).find("p").text();