【问题标题】:jquery images auto-loadingjquery 图片自动加载
【发布时间】:2023-03-20 21:48:02
【问题描述】:

我有这个代码自动从创建“相册”的目录中加载照片。目录命名为:1、2、3、4、5等,里面的照片有1.JPG、2.JPG、3.JPG等。我将它们加载到 div 元素中(tytul 是一个变量,用于存储 div 的 id,它们是 phot1、phot2 等)。现在我的问题是如何在没有图像 6.JPG 的情况下隐藏 div?

    var numbopht = 20;
var numbosubgals = 3;
var activegal = numbosubgals;
var leftnumber = 1;

var idnam = [];
//var idname = "#phot" + 


    var idunderline = "#subgal" + numbosubgals;  // subgal to id pozycji na liście




loadContent = function(i){

    var tytul = "#phot" + i;
var photurl = "../photos/" + activegal + "/" + i + ".JPG";
var img = $("<img />").attr({
src: photurl,
width:"120",
height:"90"

});
    $(tytul).empty().html(img);


};

一些不与此交互的东西

        <div id="scrollbar">
            <div id="phot20" class="photon"></div>
            <div id="phot19" class="photon"></div>
            <div id="phot18" class="photon"></div>
            <div id="phot17" class="photon"></div>
            <div id="phot16" class="photon"></div>
            <div id="phot15" class="photon"></div>
            <div id="phot14" class="photon"></div>
            <div id="phot13" class="photon"></div>
            <div id="phot12" class="photon"></div>
            <div id="phot11" class="photon"></div>
            <div id="phot10" class="photon"></div>
            <div id="phot9" class="photon"></div>
            <div id="phot8" class="photon"></div>
            <div id="phot7" class="photon"></div>
            <div id="phot6" class="photon"></div>
            <div id="phot5" class="photon"></div>
            <div id="phot4" class="photon"></div>
            <div id="phot3" class="photon"></div>
            <div id="phot2" class="photon"></div>
            <div id="phot1" class="photon"></div>
        </div>

其他东西

【问题讨论】:

    标签: jquery image autoload


    【解决方案1】:

    方法一:

    使用函数检查图像是否存在:

    function imageExists(image_url){
    var http = new XMLHttpRequest();
    http.open('HEAD', image_url, false);
    http.send();
    return http.status != 404;
    }
    

    使用函数执行进一步的操作:

    $.get(image_url)
    .done(function() {      // Image exists at specified URL and set properties
    var img = $("<img />").attr({
    src: photurl,
    width:"120",
    height:"90"
    });
    
    }).fail(function() { // Image does not exist so Hide the Div 
       $("#tytul").hide();
    })
    

    方法二:使用img的onerror事件。

    myImage.onerror = function(){
    document.getElementById("tytul").style.display = 'none';
    }
    

    【讨论】:

    • 这个解决方案我太笨了;/
    • 方法二很简单。您只需要为图像创建一个onerror 处理程序。 onerror 在图片加载失败时被调用。所以在 onerror 处理程序中简单地设置 Display:None 来隐藏 tytul div。
    • @TomaszMałachowski:这是一个代码很少的工作示例。 DEMO
    【解决方案2】:

    编辑、更新

    试试

    v3

    $("#scrollbar div[id*=phot]:not(:has(img))").hide()
    

    jsfiddle http://jsfiddle.net/guest271314/8x9bvhnj/

    【讨论】:

    • 它隐藏了所有的图片,不仅是破碎的图片
    • @TomaszMałachowski 请参阅 OP,“现在我的问题是,当没有图像 6.JPG 时如何隐藏 div?”是否需要隐藏 img 具有属性 6.JPG 或 img 父容器?可以的话可以发html吗?谢谢
    • @TomaszMałachowski 是否需要隐藏整个 phot[n] div(该 div 内的所有图像),或者,只有单个 img 在 src=6.JPG div 内具有 src=6.JPG 属性? photl[n] div 是否应该包含 6 张图片?谢谢
    • 一个 div = 一个图像,当我切换有更多图像的子画廊时,需要隐藏所有空的 div 并再次显示它们。图片的数量可以从 1 到 20 不等。
    • @TomaszMałachowski 查看更新后的帖子。在更新的 jsfiddle 中,7 个 div 包含图像,其余部分隐藏。谢谢
    猜你喜欢
    • 1970-01-01
    • 2012-02-04
    • 2016-01-07
    • 1970-01-01
    • 2017-12-02
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多