如何利用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>
<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>
document.getElementByTagName('IMG').length
var htmlCollection = document.images;
summary:
document.images returns a collection of the images in the current HTML document.
document.getElementsByTagName('IMG').length
老兄有笔误哦,UBB与str你的方法我试过可行
var str = "";
var hits = str.match(/\[img\]/ig);
alert(hits.length);
<script>
var str = "[img]";
//var hits = str.match(/\[img\]/ig);
var hits = str.match(/\[img\].*?\[\/img\]/ig);
alert(hits.length);
</script>