如何利用JS获取一段代码中图片的数量例如代码:<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>test1</title>
</head><body><p>
<img border="0" src="http://www.baidu.com/img/logo-yy.gif" width="100" height="100"></p>
<p>
<img border="0" src="http://www.baidu.com/img/logo-yy.gif" width="100" height="100"></p><table border="1" width="100%">
<tr>
<td><img border="0" src="http://www.baidu.com/img/logo-yy.gif" width="100" height="100"></td>
</tr>
</table><p>
<img border="0" src="http://www.baidu.com/img/logo-yy.gif" width="100" height="100"></p></body></html>

解决方案 »

  1.   

    document.images.length
    document.getElementByTagName('IMG').length
      

  2.   


    var htmlCollection = document.images;
    summary:
    document.images returns a collection of the images in the current HTML document.
      

  3.   

    re:
    document.getElementsByTagName('IMG').length
      

  4.   

    好像不行呢如果是UBB格式呢,例如
      

  5.   

    源代码是放在<textarea></textarea>中的,用document.images.length获取不到,其实我主要是想获取<textarea></textarea>中的[img]图片数量
      

  6.   

    引用 5 楼 lwife 的回复:
    源代码是放在 <textarea> </textarea>中的,用document.images.length获取不到,其实我主要是想获取 <textarea> </textarea>中的
    var hits = str.match(/\[img\]/ig);
    alert(hits.length);
      

  7.   


    老兄有笔误哦,UBB与str你的方法我试过可行
    var str = "";
    var hits = str.match(/\[img\]/ig);
    alert(hits.length);
      

  8.   

    var hits = str.match(/\[img\].*?\[\/img\]/ig);
      

  9.   

    非常不错,感谢大家,问题搞定,结帖闪人
    <script>
    var str = "[img]";
    //var hits = str.match(/\[img\]/ig);
    var hits = str.match(/\[img\].*?\[\/img\]/ig);
    alert(hits.length);
    </script>