返回的是object HTMLCollection怎么解决

时间:2022-10-24 21:29:48
var imgs=document.getElementById("J_UlThumb").getElementsByTagName("img");

获取图片返回的是object HTMLCollection

如果输出imgs显示为object HTMLImageElement
怎么才能得到图片里的src

4 个解决方案

#1


document.getElementById("J_UlThumb").getElementsByTagName("img")[0],试试

#2



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<img src="http://www.apobates.com/big.logo.png"/>
<script type="text/javascript">
var eles=document.getElementsByTagName('img');
//alert(eles[0]);
alert(eles[0].src);
</script>
</body>
</html>

#3


返回的是object HTMLCollection怎么解决

#4


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>test1</title>
    <script src="Scripts/jquery-1.7.1.min.js"></script>
    <script type="text/javascript">
        window.onload = function () {
            //DOM方法
            var imgs = document.getElementById("J_UlThumb").getElementsByTagName("img");
            for (var i = 0; i < imgs.length; i++) {
                alert(imgs[i].src);
            }

            //jquery方法
            $("#J_UlThumb").find("img").each(function () {
                alert($(this).attr("src"));
            });
        }
    </script>
</head>
<body>
    <div id="J_UlThumb">
        <img id="Img1" src="img1.jpg" />
        <img id="Img2" src="img2.jpg" />
    </div>
</body>
</html>

#1


document.getElementById("J_UlThumb").getElementsByTagName("img")[0],试试

#2



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<img src="http://www.apobates.com/big.logo.png"/>
<script type="text/javascript">
var eles=document.getElementsByTagName('img');
//alert(eles[0]);
alert(eles[0].src);
</script>
</body>
</html>

#3


返回的是object HTMLCollection怎么解决

#4


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>test1</title>
    <script src="Scripts/jquery-1.7.1.min.js"></script>
    <script type="text/javascript">
        window.onload = function () {
            //DOM方法
            var imgs = document.getElementById("J_UlThumb").getElementsByTagName("img");
            for (var i = 0; i < imgs.length; i++) {
                alert(imgs[i].src);
            }

            //jquery方法
            $("#J_UlThumb").find("img").each(function () {
                alert($(this).attr("src"));
            });
        }
    </script>
</head>
<body>
    <div id="J_UlThumb">
        <img id="Img1" src="img1.jpg" />
        <img id="Img2" src="img2.jpg" />
    </div>
</body>
</html>