【问题标题】:Replace broken image icon with text用文本替换损坏的图像图标
【发布时间】:2014-08-08 23:34:38
【问题描述】:

我想在损坏的图像图标弹出时隐藏或删除它,并将其替换为以下文字:“图像错误”。现在,我有一些 jQuery 可以隐藏损坏的图像图标,但我无法以这种方式设置和显示 alt 文本。我是否需要替换图像,或者是否有办法在隐藏损坏的图像图标时显示文本?

这是 jQuery 代码:

<script type="text/javascript">
    $(document).ready(function () {
        $("img").error(function () {
            $(this).hide();
        })
    .attr("src", "error with image");
    });
</script>

这些是表格中用于显示图像缩略图的字段:

<asp:ImageField HeaderText="Line dwg Thumb" DataImageUrlField="LineDrawThumbnail" NullDisplayText="error with image"  ControlStyle-Width="75px" ItemStyle-HorizontalAlign="Center">
 </asp:ImageField>
 <asp:ImageField HeaderText="Profile Thumb" DataImageUrlField="ProfileThumbnail" NullDisplayText="error with image" ControlStyle-Width="75px"  ItemStyle-HorizontalAlign="Center">
  </asp:ImageField>           

重要的附注:包含缩略图图像的数据库表没有任何用于配置文件缩略图或线条绘制缩略图的空白字段 - 碰巧有时缩略图尚不存在。

【问题讨论】:

  • 为什么不将属性 alt="error with image" 添加到图像中?
  • 为什么不能使用alt?
  • 如果您知道有时缩略图不存在,您应该设置一个默认的图像来说明这一点。然后,如果图像在那里,则覆盖它。您不能依赖任何标签自动在所有浏览器上正确显示。
  • 我尝试过使用替代文字,但仍然显示损坏的图像图标。
  • @SaraDeJaneiro 为什么不修复 URL?似乎是唯一体面的事情。

标签: c# javascript jquery brokenimage


【解决方案1】:

您的脚本非常好,您只需在隐藏图像后添加文本

<script type="text/javascript">
    $(document).ready(function () {
        $("img").error(function () {
            $(this).hide();
        })
    .after('<p>error with image</p>');
    });
</script>

【讨论】:

    猜你喜欢
    • 2020-08-30
    • 2012-02-09
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 2010-09-10
    • 1970-01-01
    相关资源
    最近更新 更多