【问题标题】:Hide img tag if no image added from admin side如果没有从管理端添加图像,则隐藏 img 标签
【发布时间】:2019-08-23 23:00:08
【问题描述】:

如果没有从管理端添加图像,我希望能够隐藏 img src 标签

我已经尝试过onerror属性

这是我尝试过的

if ($row['postImage'] !=''){
    echo "<p><img src='".$url.$row['postImage']."' class='img-responsive' onerror='this.style.display='none';'></p>";
}

上面的代码只是输出以下内容

<img src='https://www.it-doneright.co.uk/blog/images/' class='img-responsive' onerror='this.style.display='none';'>

更新:可以使用以下代码

 if ($row['postImage']!='images/' && $row['postImage']!=''){
 echo "<p><img src='".$url.$row['postImage']."' class='img-responsive'> 
 </p>"; 
 }

【问题讨论】:

    标签: php image src


    【解决方案1】:

    我认为您的 postImage 字段应该有一个“null”默认值,这样就足以测试真假。 问候。

    【讨论】:

    • 我已将 postImage 设置为 NULL 作为 db 表中的默认值,但不确定编码是否诚实
    • DB 中默认值为空,您的条件应为if ($row['postImage']){ echo "&lt;p&gt;&lt;img src='".$url.$row['postImage']."' class='img-responsive' onerror='this.style.display='none';'&gt;&lt;/p&gt;"; }
    • 我尝试了该代码,但仍然显示损坏的图像占位符,并且源代码中仍然显示 img 标签
    • 嗯,这对我来说听起来很奇怪,我刚刚使用两个变体复制了您的代码来测试“$row['postImage'”变量,但没有显示任何内容。你可以在这里看到它faridsilva.com/test-image.php
    • 现在设法对问题进行了排序,即将用工作代码更新原始代码
    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 2016-09-10
    相关资源
    最近更新 更多