简体   繁体   English

jQuery检查锚点中的图片是否等于特定的网址

[英]jquery check if image in anchor is equal to specific url

I just want to check if image in anchor is equal to specific url. 我只想检查锚点中的图片是否等于特定的网址。 Here is html i have 这是我有的HTML

<div class="gallery_images" id="2">
                            <a href="" data-toggle="modal" data-target="#myModal1" tabindex="0">
                                <img class="img-fluid" src="http://v9contest.geojidesign.com/wp-content/uploads/contest_entries/thumbnail-^739D24706DC178C938623C638DDBFF0BC04ADF35338114AF68^pimgpsh_thumbnail_win_distr.jpg">
                            </a>
                        </div>

so when page loads, i want to suppose check if link image in anchor is equal to http://google.com . 所以当页面加载时,我想假设检查锚中的链接图像是否等于http://google.com Here what i tried 这是我尝试过的

if(jQuery('a').has('img.img-fluid').attr('src')=='http://google.com'){ alert('ok'); }

You may use the direct descendent selector, > , instead. 您可以使用直接后代选择器> Following is the working example: 以下是工作示例:

 if (jQuery('a > img.img-fluid').attr('src') == 'http://google.com') { alert('ok'); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="gallery_images" id="2"> <a href="" data-toggle="modal" data-target="#myModal1" tabindex="0"> <img class="img-fluid" src="http://google.com"> </a> </div> 

Keep in mind that the selector may return more than one element if there are many such elements. 请记住,如果有许多这样的元素,则选择器可能返回多个元素。 In that case, you will need to loop through and check. 在这种情况下,您将需要循环检查。

Thanks, it is resoved. 谢谢,它已保存。 I used 我用了

jQuery('a > img.img-fluid').each(function(){
            if(jQuery(this).attr('src')=='http://google.com'){ alert('ok'); }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM