javascript - 如何将图像附加到 div 然后将其清空然后再次附加回它?

标签 javascript jquery html css

我正在使用 appendingTo 将图像 (img1) 附加到 div,然后我想清空 div 中的 (img1) 并向其附加其他内容 (img2)。但是当我再次尝试附加 (img1) 时,什么也没有出现。

<div id="countries">
    <div class="fixed">
        <div class="firstRow display all" id="nz">
            <h2>New Zealand</h2>
            <p>Awesome cities and nature</p>
        </div>
        <div class="firstRow display all" id="aust"></div>
        <div class="firstRow display all" id="vietnam"></div>
        <div class="clear"></div>
        <div class="secondRow display all" id="fiji"></div>
        <div class="secondRow display all" id="sing"></div>
        <div class="secondRow display all" id="raro"></div>
    </div>

</div>
<div id="cities">
    <div class="fixed">
        <div id="left">
            <div class="bigLeft" id="zero"></div>
            <div class="smallLeft" id="one" style="margin-left: 0px"></div>
            <div class="smallLeft" id="two"></div>
            <div class="smallLeft" id="three"></div>
            <div class="smallLeft" id="four" style="margin-left: 0px;"></div>
            <div class="smallLeft" id="five"></div>
        </div>
        <div id="rightBar">
            <div id="city1" class="citiesPlace" style="margin-top:0px;"></div>
            <div id="city2" class="citiesPlace" style="margin-top:0px;"></div>
            <div id="city3" class="citiesPlace" style="margin-top:0px;"></div>
            <div id="city4" class="citiesPlace"></div>
            <div id="city5" class="citiesPlace"></div>
            <div id="city6" class="citiesPlace"></div>
            <div id="city7" class="citiesPlace"></div>
            <div id="city8" class="citiesPlace"></div>
            <div id="city9" class="citiesPlace"></div>
        </div>
    </div>
</div>

<div id="nzImg0" style="display:none;">
    <img src="Images/aust.jpg" style="width: 219px; height: 150px">
</div>
<div id="nzImg1" style="display:none;">
    <img src="Images/nz.jpg" style="width: 219px; height: 150px">
</div>
<div id="nzImg2" style="display:none;">
    <img src="Images/aust.jpg" style="width: 219px; height: 150px">
</div>
<div id="nzImg3" style="display:none;">
    <img src="Images/aust.jpg" style="width: 219px; height: 150px">
</div>
<div id="nzImg4" style="display:none;">
    <img src="Images/aust.jpg" style="width: 219px; height: 150px">
</div>


<div id="austImg0" style="display:none;">
    <img src="Images/aust.jpg" style="width: 219px; height: 150px">
</div>
<div id="austImg1" style="display:none;">
    <img src="Images/aust.jpg" style="width: 219px; height: 150px">
</div>
<div id="austImg2" style="display:none;">
    <img src="Images/aust.jpg" style="width: 219px; height: 150px">
</div>

jQuery 部分

$(".all").click(function () {
    $("#countries").fadeOut(500);
    $("#cities").fadeIn(500);

    var j = 1;
    var f = 0;
    for (var i = 0; i < a.length; i++) {

        if (a[i] == $(this).attr("id")) {

            $(this).appendTo("#zero");
            $(this).find("h2").css("fontSize", "300%");
            $(this).find("p").css("fontSize", "80%");
            $(this).css("height", "100%");
            $(this).css("width", "100%");

            for (var m = 0; m < c.length; m++) {
                $("#" + c[m]).empty();
            }

            for (var f = 0; f < c.length; f++) {
                $("#" + a[i] + "Img" + f + " " + "img").appendTo("#" + c[f]);
                $("#" + a[i] + "Img" + f + " " + "img").css("display", "block");

            }
        } else if (a[i] != $(this).attr("id")) {

            $("#" + a[i]).appendTo("#" + b[j]);
            $("#" + a[i] + " " + "h2").css("fontSize", "100%");
            $("#" + a[i] + " " + "p").css("fontSize", "80%");
            $("#" + a[i]).css("height", "100%");
            $("#" + a[i]).css("width", "100%");
            j++;
        }
    }
});

最佳答案

当您将元素附加到 div 中时,您正在移动它。当你移动它时,原来的就不再存在了。当您清空 div 时,它就会消失。所以现在你不能引用它。

因此,您要么不需要删除该元素而只是将其隐藏,要么不将其清空,而是将该元素移回原来的位置。

关于javascript - 如何将图像附加到 div 然后将其清空然后再次附加回它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34302700/

相关文章:

javascript - 拒绝加载脚本,因为它违反了以下内容安全策略指令

php - 如何在 <li> 中将 Bootstrap 徽章与 AJAX 对齐

javascript - Ajax 和 VbScript : fetching Post variable in test. asp

javascript 变量未显示正确的值

jQuery 窗口大小

javascript - Jquery Slider - 半步视觉条长度

javascript - 填充字母数字数组元素之间的间隙

javascript - Firebase 数据更改时 Google Charts 不会更新

javascript - 将复选框设置为选中前 2 个表行中的输入字段

html - src 属性仅适用于相对路径