这里是我的htmldocument.getElementsByTagName(“img”);与document.getElementById('testimg');
<a href="index.php"><img id="testimg" src="images/logo.png"/></a>
这里是我的javascript
function getW(){
var theImg = document.getElementById('testimg');
return theImg;
}
theImg = getW();
if (theImg.width > 119){
document.write(theImg.width);
}
现在,当我使用这个脚本出来放IMG宽度
然而,当我使用这个脚本
function getW(){
var theImg = document.getElementsByTagName("img");
return theImg;
}
theImg = getW();
if (theImg.width > 119){
document.write(theImg.width);
}
它不输出任何东西。有什么区别,为什么这第二个脚本工作?
谢谢!
如果我想返回页面上所有的img宽度,该怎么办? – Chris 2011-04-28 03:38:24
@Chris然后,您将对'getElementsByTagName()'返回的集合使用循环。 – alex 2011-04-28 03:39:15