【发布时间】:2018-02-16 21:36:50
【问题描述】:
我有这个基本的搜索系统,其中包含“卡片”中的文章,如 here 有些文章包含图片,有些则没有。
我只想在“图片框”div 中显示图片时,instead of having a blank white space when there's no picture.
如何只删除没有图片的“图片框”div?
PHP:
$sql = "SELECT * FROM article ORDER BY a_id DESC LIMIT 3";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
if ($queryResults > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<a href='article.php?title=".$row['a_title']."&date=".$row['a_date']."'>
<div class='article-wrapper'>
<div class='picture-box'>
<img src='".$row['picture']."' class='picture'>
</div>
<p class='text'>".$row['text']."</p>
</div>
</a>";
}
}
CSS:
.picture {
position:absolute;
left: -100%;
right: -100%;
top: -100%;
bottom: -100%;
margin: auto;
max-width: 100%;
height: auto;
}
.picture-box {
width: 560px;
height: 300px;
overflow: hidden;
position: relative;
}
我试过这个:
PHP:
<img src='".$row['picture']."' class='picture' onerror="imgError(this)">
jquery:
function imgError(img) {
img.error="";
$(img).parent().remove();
}
-但它会从所有文章中删除“图片框”div,而不是特定文章。
【问题讨论】:
-
我认为按照您的做法,最好从服务器端删除。而且 $('img') 应该是 $(img)