【问题标题】:creating an array and looping through it in javascript/jquery创建一个数组并在 javascript/jquery 中循环遍历它
【发布时间】:2023-03-23 22:45:01
【问题描述】:

我创建了一个数组。我想把东西推到数组中,当我点击“pic2”时让它运行“runShadow”函数......我希望该函数使任何已推入数组的对象可见:

<script type="text/javascript">
$(document).ready(function () {
    var shadowArray = [];

    function runShadow() { // jquery- ".each"? look this up in stackoverflow!
        console.log("functionWorking!");
        for (var i = 0; i < shadowArray.length; i++) {
            shadowArray[i].animate({
                opacity: 1
            }, 1500, "linear");
        }
    }
    $("#pic1a").click(function () {
        $('#pic2').fadeIn(1500);
        $('#pic1').animate({
            top: -100,
            left: -1000
        }, 1500, "linear");
        $('#pic1a').animate({
            opacity: 0
        }, 1500, "linear");
        $('#pic1b').animate({
            opacity: 0
        }, 1500, "linear");
        $('#pic1c').animate({
            opacity: 0
        }, 1500, "linear");
        $('#pic1d').animate({
            opacity: 0
        }, 1500, "linear");
        $('#pic1e').animate({
            opacity: 0
        }, 1500, "linear");
        $('#pic1f').animate({
            opacity: 0
        }, 1500, "linear");

        shadowArray.push("pic1a");
        console.log(shadowArray);
        console.log("WORKING!");

    });
    $("#pic2").click(function () {
        $('#pic2').fadeOut(1500);
        $('#pic1').animate({
            top: -770,
            left: -800
        }, 1500, "linear");
        runShadow();
    });
});
</script>

【问题讨论】:

    标签: javascript jquery arrays loops


    【解决方案1】:

    试试

    $("#"+shadowArray[i]).animate(...)
    

    【讨论】:

    • 是的,做到了!谢谢!...如果您看到此评论,您介意告诉我标签的用途吗?我不认为我理解那部分......我确实看到数组必须被包裹在 jquery 中......非常酷。再次感谢!!!
    猜你喜欢
    • 1970-01-01
    • 2010-11-22
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    相关资源
    最近更新 更多