admin管理员组文章数量:1410724
I am loading XML feed from another site with PHP. My output in HTML is like this:
<div class="sponsored">
<div class='box'><img src='http:///xxx.jpg'><a href='xxx'>Title</a></div>
<div class='box'><img src='http:///xxx.jpg'><a href='xxx'>Title</a></div>
<div class='box'><img src='http:///xxx.jpg'><a href='xxx/'>Title</a></div>
<div class='box'><img src=''><a href='xxx'>Title</a></div>
</div>
The last part has no src url, meaning the feed didn't provide any image. I would like to check this HTML output with jquery to check if there's no src url. and if so, then put defauly image for better formatting.
I saw jquery scripts that check for images within div. But is there a way to check if images src is empty or not?
================
UPDATE:
Ok, I'm not sure why it didn't work before. Finally the code below works on my end:
$(".sponsored > .box > img[src='']").attr("src","default.png");
Thank You All for Answers!! Here's final working code for future noobs like me :)
<script>
$(document).ready(function(){
$(".sponsored > .box > img[src='']").attr("src","image.jpg");
});
</script>
I am loading XML feed from another site with PHP. My output in HTML is like this:
<div class="sponsored">
<div class='box'><img src='http:///xxx.jpg'><a href='xxx'>Title</a></div>
<div class='box'><img src='http:///xxx.jpg'><a href='xxx'>Title</a></div>
<div class='box'><img src='http:///xxx.jpg'><a href='xxx/'>Title</a></div>
<div class='box'><img src=''><a href='xxx'>Title</a></div>
</div>
The last part has no src url, meaning the feed didn't provide any image. I would like to check this HTML output with jquery to check if there's no src url. and if so, then put defauly image for better formatting.
I saw jquery scripts that check for images within div. But is there a way to check if images src is empty or not?
================
UPDATE:
Ok, I'm not sure why it didn't work before. Finally the code below works on my end:
$(".sponsored > .box > img[src='']").attr("src","default.png");
Thank You All for Answers!! Here's final working code for future noobs like me :)
<script>
$(document).ready(function(){
$(".sponsored > .box > img[src='']").attr("src","image.jpg");
});
</script>
Share
Improve this question
edited Mar 9, 2016 at 12:21
reizer
asked Mar 9, 2016 at 11:46
reizerreizer
2531 gold badge4 silver badges12 bronze badges
0
5 Answers
Reset to default 2You can do it with attribute equals
selector,
$(".sponsored > .box > img[src='']").attr("src","default.png");
$(document).ready(function(){
$(".sponsored .box img").each(function(){
if($(this).attr('src') == ''){
$(this).attr('src') = 'default.png';
}
});
});
You can use like that, this example will give you 3 not empty
records and 1 empty
record, you can check the result in browser console:
$('.box img').each(function(index,val){
if($(this).attr('src') == ''){
console.log('empty');
}
else{
console.log('not empty');
}
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sponsored">
<div class='box'><img src='http:///xxx.jpg'><a href='xxx'>Title</a></div>
<div class='box'><img src='http:///xxx.jpg'><a href='xxx'>Title</a></div>
<div class='box'><img src='http:///xxx.jpg'><a href='xxx/'>Title</a></div>
<div class='box'><img src=''><a href='xxx'>Title</a></div>
</div>
try this
$('.box img').each(function(){
if($(this).attr('src') == ''){
//do something
}
else{
//do something
}
});
$('.sponsored .box img').each(function () {
if (this.src!== undefined && this.src!='') {
console.log("Exists");
}else
{
console.log("Not Exists");
}
});
本文标签: javascriptHow to Check with jQuery if Image URL is Empty and Do somethingStack Overflow
版权声明:本文标题:javascript - How to Check with jQuery if Image URL is Empty and Do something? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744794815a2625516.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论