如果 src 为空,则隐藏图像 [英] Hide image if src is empty

查看:36
本文介绍了如果 src 为空,则隐藏图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果源为空,我试图隐藏 .但我没有运气.

I'm trying to hide <img> if the source is empty. But I have no luck.

我在这里找到的帖子很少,但对我不起作用.

I found few posts here, but it doesn't work for me.

这是我的代码,它是基于表格的,因为它将是一个模板:图片:

Here is my code, it's table based because it will be a template: Images:

<td width="92%" align="center" class="imagenes_desc">
        <a href="#" class="showcase"><img class="imagen" src="http://webs.ono.com/norfolk/ebay/images/01.jpg" width="800"></a>
        <a href="#" class="showcase"><img class="imagen" src="" width="800"></a>
        <a href="#" class="showcase"><img class="imagen" src="" width="800"></a>
        <a href="#" class="showcase"><img class="imagen" src="" width="800"></a>
        <a href="#" class="showcase"><img class="imagen" src="" width="800"></a>
        <a href="#" class="showcase"><img class="imagen" src="" width="800"></a>
        <a href="#" class="showcase"><img class="imagen" src="" width="800"></a>
        <a href="#" class="showcase"><img class="imagen" src="" width="800"></a>
        <a href="#" class="showcase"><img class="imagen" src="" width="800"></a>
        <a href="#" class="showcase"><img class="imagen" src="" width="800"></a>
        </td>

这是我正在尝试实现的 Javascript.

Here is the Javascript I'm trying to implement.

<script type="text/javascript">
        $(document).ready(function(){
            if ($(".imagen").attr(src="") == "") {
                $(".imagen").hide();
            }
            else {
                $(".imagen").show();
            }
</script>

我对 JS 不是很熟悉,我在 Stackoverflow 上找到了这个脚本,但我无法让它工作.

I'm not very familiar with JS, I found this script here on Stackoverflow, but I can't get it to work.

更新

尝试此操作,但不起作用(Chrome 隐藏得很好,但 Firefox 和 IE 不行):

Trying this, but doesn't work (Chrome hides well, but Firefox and IE don't):

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type="text/javascript">
$("imagen").each(function(){

  if ($(this).attr("src") == "") 
       $(this).hide();
  else

      $(this).show();
});
</script>
<style>
.hide {display:none !important;}
.show {display:block !important;}
</style>

谢谢,

推荐答案

如何使用 jQuery 的属性选择器?

    $(document).ready(function(){
        $('.imagen[src=""]').hide();
        $('.imagen:not([src=""])').show();
    });

工作示例

这篇关于如果 src 为空,则隐藏图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆