在网上找到这个函数:
使用此函数能返回字符串中某个字符或字符串出现的次数其中mainStr为要查找的字符串,subStr为要检查的字符串或字符请问,在html里如何调用countInstances函数?如何让子串出现的次数显示在一个<div>标签里?function countInstances(mainStr, subStr)
    {
        var count = 0;
        var offset = 0;
        do
        {
            offset = mainStr.indexOf(subStr, offset);
            if(offset != -1)
            {
                count++;
                offset += subStr.length;
            }
        }while(offset != -1)
        return count;
    }我用下面两种方法调用,第一个方法没有结果,第二个方法结果正常:<html xmlns="http://www.w3.org/1999/xhtml">   
<head>   
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><meta http-equiv="refresh" content="20">
<!-- 页面自动刷新:把如下代码加入<head>区域中<meta http-equiv="refresh" content="20">,其中20指每隔20秒刷新一次页面 -->
<title>js返回字符串中某个字符的出现次数</title>
<script type="text/javascript">/*使用此函数能返回字符串中某个字符或字符串出现的次数其中mainStr为要查找的字符串,subStr为要检查的字符串或字符*/
function countInstances(mainStr, subStr)
    {
        var count = 0;
        var offset = 0;
        do
        {
            offset = mainStr.indexOf(subStr, offset);
            if(offset != -1)
            {
                count++;
                offset += subStr.length;
            }
        }while(offset != -1)
        return count;
    } 
</script> 
</head>
<body>   
<div>
<!-- 方法一 -->
<Javascript type="text/javascript"> 
alert(countInstances('china_china_nihao_china','china')); 
</Javascript></br><!-- 方法二 -->
<a href="javascript:alert(countInstances('china_china_nihao_china','i'))">点击查看子串出现的次数</a>
 
</div>
</body>   
</html>

解决方案 »

  1.   

    <html xmlns="http://www.w3.org/1999/xhtml">   
    <head>   
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><meta http-equiv="refresh" content="20">
    <!-- 页面自动刷新:把如下代码加入<head>区域中<meta http-equiv="refresh" content="20">,其中20指每隔20秒刷新一次页面 -->
    <title>js返回字符串中某个字符的出现次数</title>
    <script type="text/javascript">/*使用此函数能返回字符串中某个字符或字符串出现的次数其中mainStr为要查找的字符串,subStr为要检查的字符串或字符*/
    function countInstances(mainStr, subStr)
        {
            var count = 0;
            var offset = 0;
            do
            {
                offset = mainStr.indexOf(subStr, offset);
                if(offset != -1)
                {
                    count++;
                    offset += subStr.length;
                }
            }while(offset != -1)
            return count;
        } 
    </script> 
    </head>
    <body>   
    <div>
    <!-- 方法一 -->
    <script type="text/javascript"> alert(countInstances('china_china_nihao_china','china')); 
    </script>
    </br><!-- 方法二 -->
    <a href="javascript:alert(countInstances('china_china_nihao_china','i'))">点击查看子串出现的次数</a>
     
    </div>
    </body>   
    </html>
      

  2.   

    <html xmlns="http://www.w3.org/1999/xhtml">   
    <head>   
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><meta http-equiv="refresh" content="20">
    <!-- 页面自动刷新:把如下代码加入<head>区域中<meta http-equiv="refresh" content="20">,其中20指每隔20秒刷新一次页面 -->
    <title>js返回字符串中某个字符的出现次数</title>
    <script type="text/javascript">
    /*使用此函数能返回字符串中某个字符或字符串出现的次数其中mainStr为要查找的字符串,subStr为要检查的字符串或字符*/
    function countInstances(mainStr, subStr)
        {
            var count = 0;
            var offset = 0;
            do
            {
                offset = mainStr.indexOf(subStr, offset);
                if(offset != -1)
                {
                    count++;
                    offset += subStr.length;
                }
            }while(offset != -1)
            return count;
        } 
    </script>
    </head>
    <body>
    <div id="kk">
    <!-- 方法一 -->
    <script type="text/javascript"> 
    document.write(countInstances('china_china_nihao_china','china')); 
    </script>
    </br>
    <!-- 方法二 -->
    <a href="javascript:document.getElementById('kk').innerText =countInstances('china_china_nihao_china','i')">点击显示子串出现的次数</a>
    </div>
    </body>   
    </html>