Javascript - 如何检查元素是否包含图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15591942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:19:39  来源:igfitidea点击:

Javascript - How can I check if an element contains an image?

javascripthtml

提问by jord49

Can anyone suggest how I could check if an element contains an image? I'm assuming it would be whether or not it contains the image source but am unsure how to code this.

谁能建议我如何检查元素是否包含图像?我假设它是否包含图像源,但我不确定如何编码。

采纳答案by Amol Fasale

You can get the image elemant by using js getElementById then you can easily access the src of that image, which tells you weather element contains image if src is not empty or if contains then src should have image path.

您可以使用 js getElementById 获取图像元素,然后您可以轻松访问该图像的 src,如果 src 不为空,它会告诉您天气元素包含图像,或者如果包含,则 src 应该具有图像路径。

   <html>
    <head>
    <script>
    function myFunction()
    {
     var imgSrc = document.getElementById("myImg").attr('src');
     alert(imgSrc);
    }
    </script>
    </head>

    <body>

    <h1>My Web Page</h1>

    <p id="demo">A Paragraph</p>

    <img id="myImg" src="http://www.blah.com/img.jpg" onclick="myFunction()"></img>

    </body>

</html>

回答by Niet the Dark Absol

You can do this like so:

你可以这样做:

if( elem.getElementsByTagName('img').length > 0) {
    // there is an image
}


EDIT: After seeing your comment, try this:

编辑:看到你的评论后,试试这个:

var imgs = elem.getElementsByTagName('img'), len = imgs.length, i;
for( i=0; i<len; i++) {
    if( imgs[i].src.match(/\/image\.jpg$/)) {
        // the image is there
        break;
    }
}

回答by Ifnot

If you are using jQuery you can do something like that :

如果您使用 jQuery,您可以执行以下操作:

if($('#container_id').find('img').length > 0) {
     // Image found on container with id "container_id"
}

回答by Adam Plocher

This should work and won't require JQuery like Anael's solution:

这应该可以工作,并且不需要像 Anael 的解决方案那样的 JQuery:

<div id="test1"><img src="img.jpg" /></div>
<div id="test2">Hello</div>    

<script>
function hasImage(id) {
    var childElements = document.getElementById(id).childNodes;
    for (var i = 0; i < childElements.length; i++) {
        if (childElements[i].localName != null && childElements[i].localName.toLowerCase() == "img") {
            return true;
        }
    }
    return false;
}

alert(hasImage('test1')); // true
alert(hasImage('test2')); // false
</script>

Demo at JSFiddle: http://jsfiddle.net/qLPJC/

JSFiddle 演示:http: //jsfiddle.net/qLPJC/

Update:

更新:

To see if it has a specific srctry this update:

要查看它是否有特定的src尝试此更新:

function hasImage(id) {
    var childElements = document.getElementById(id).childNodes;
    for (var i = 0; i < childElements.length; i++) {
        if (childElements[i].localName != null && childElements[i].localName.toLowerCase() == "img") {
            if (childElements[i].getAttribute('src') != null && childElements[i].getAttribute('src').toLowerCase() == "img.jpg") {
                return true;
            }
        }
    }
    return false;
}

alert(hasImage('test1'));
alert(hasImage('test2'));

Updated JS Fiddle example: http://jsfiddle.net/qLPJC/2/

更新的 JS 小提琴示例:http: //jsfiddle.net/qLPJC/2/

回答by lbdm44

something like

就像是

$("div:has(img)")

if you are using jquery

如果你使用 jquery