【问题标题】:jQuery not calculating total height div's dynamically added children elementsjQuery不计算总高度div的动态添加的子元素
【发布时间】:2017-05-07 16:51:07
【问题描述】:

能否请您帮助了解它为什么不工作并帮助使其工作?

我正在尝试计算动态添加/放置到 DIV 元素的所有子元素的总高度,代码不起作用 [即子元素的数量每次都会有所不同]。当我在 console.log() 上检查返回值时,代码返回初始定义值零。

这是代码和打印屏幕:

 //This is the code that dynamically extracts data every second from the server and populates it into to DIV element.
            setInterval(function() {
                google.script.run.withSuccessHandler(function(data) {
                    var temp3 = '';
                    for (var i = 0; i < data.length; i++) {
                        temp3 += '<p>' + data[i].join() + '</p>';
                    }
                    document.getElementById("item1").innerHTML = temp3;
                }).getReadyLine();
            }, 1000);

//This is the code that should calculate the total height of all the children elements of DIV element. But it is not working, not calculating, it displays 0 on the console.log().
            var outerHeight1 = 0;
            $("#item1").children().each(function() {
                outerHeight1 += $(this).outerHeight();
            });
        </script>
//This is the code for DIV.
        <div class="item1" id="item1">

附言

Update: included the code inside setInerval like this:
<script>

    $( document ).ready(function () {
        setInterval(function() {
            google.script.run.withSuccessHandler(function(data) {
                var temp3 = '';
                for (var i = 0; i < data.length; i++) {
                    temp3 += '<p>' + data[i] + '</p>';
                }
                document.getElementById("item1").innerHTML = temp3;
            }).getReadyLine();
            var outerHeight1 = 0;
                $("#item1").children().each(function() {
                  outerHeight1 += $(this).outerHeight(true);
                  return outerHeight1;
                });
                console.log(outerHeight1);
        }, 1000);

        /*
        var outerHeight1 = 0;
        $("#item1").children().each(function() {
            outerHeight1 += $(this).outerHeight();
        }); */

        var outerHeight2 = 0;
        $("#item2").children().each(function() {
            outerHeight2 += $(this).outerHeight(true);
        })

        $("#item1").animate({
            scrollTop: outerHeight1
        }, 10000);
        setTimeout(function() {
            $("#item1").animate({
                scrollTop: 0
            }, 10000);
        }, 3000);
        setInterval(function() {
            // 4000 - it will take 4 secound in total from the top of the page to the bottom
            $("#item1").animate({
                scrollTop: outerHeight1
            }, 10000);
            setTimeout(function() {
                $("#item1").animate({
                    scrollTop: 0
                }, 10000);
            }, 4000);

        }, 4000);

        $("#item2").animate({
            scrollTop: outerHeight2
        }, 8000);
        setTimeout(function() {
            $("#item2").animate({
                scrollTop: 0
            }, 8000);
        }, 8000);
        setInterval(function() {
            // 4000 - it will take 4 secound in total from the top of the page to the bottom
            $("#item2").animate({
                scrollTop: outerHeight2
            }, 8000);
            setTimeout(function() {
                $("#item2").animate({
                    scrollTop: 0
                }, 8000);
            }, 8000);

        }, 8000);

        console.log($("#item1").outerHeight());
        console.log(outerHeight1);
        });




    </script>

【问题讨论】:

  • 如果您可以在这里发布小提琴或代码sn-p,那将会很有帮助。
  • 问题是你只计算了一次高度,并且在任何元素添加到#item1之前完成了
  • 正是...您在setInterval之外进行操作
  • 什么console.log()?您可能在setInterval 回调之外调用了console.log(outerHeight1),这意味着该语句在设置outerHeight1 之前执行。只需花点时间了解每个函数的执行顺序和每个变量的上下文。
  • 我刚刚将代码包含在 setInterval 中,它可以工作,但在外部还有另一个 setInterval 重复动画(向上和向下滚动),没有看到 outerHeight 并显示错误:outerHeight1 未定义:请在最后的 PS 部分中查看上面消息中的更新代码。谢谢。

标签: javascript jquery html height children


【解决方案1】:

感谢您对一次性计算和在 setInterval 部分中嵌入代码的提示。

这是有效的代码:

$(document).ready(function() {
            $("#item1").animate({
                scrollTop: $("#item1").height()
            }, 4000);

            setTimeout(function() {
                $("#item1").animate({
                    scrollTop: 0
                }, 4000);
            }, 4000);

            setInterval(function() {
                // 4000 - it will take 4 secound in total from the top of the page to the bottom
                $("#item1").animate({
                    scrollTop: $("#item1").height()
                }, 4000);
                setTimeout(function() {
                    $("#item1").animate({
                        scrollTop: 0
                    }, 4000);
                }, 4000);

            }, 8000);
        });

        var totalHeight = 0;

        $("#leftcolumn").children().each(function() {
            totalHeight = totalHeight + $(this).outerHeight(true);
        });

        var outerHeight = 0;
        $('.profile').each(function() {
            outerHeight += $(this).outerHeight();
        });
        $("#total-height").text(outerHeight + 'px');

        setInterval(function() {
            google.script.run.withSuccesshandler(function(data) {
                $("input[type=text]").each(function() {
                    out += "<li>" + $(this).val() + "</li>";
                });
                out += "</ul>";

                $('#listView').html(out);
            }).getReadyLine();
        }, 50000);

        $('#mybutt').click(function() {
            var out = '<ul>';
            $("input[type=text]").each(function() {
                out += "<li>" + $(this).val() + "</li>";
            });
            out += "</ul>";

            $('#listView').html(out);
        });
        setInterval(function() {
            google.script.run.withSuccessHandler(function(data) {
                console.log(data);
                document.getElementById("div").innerHTML = whatToWrite;
            }).getReadyLine();
        }, 1000);

        setInterval(function() {
            google.script.run.withSuccessHandler(function(data) {
                console.log(data);
                var temp3 = '';
                for (var i = 0; i < data.length; i++) {
                    temp3 = 'p' + i + '/p';
                    document.getElementById("#item1").innerHTML = temp3;
                }
            }).getReadyLine();
        }, 1000);


        setInterval(function() {
            google.script.run.withSuccessHandler(function(data) {
                var temp3 = '';
                for (var i = 0; i < data.length; i++) {
                    temp3 = 'p' + data[i] + '/p';
                    document.getElementById("#item1").innerHTML = temp3;
                    temp3 = '';
                }
            }).getReadyLine();
        }, 1000);

        //This is the server side code that returns the data, it works fine, have checked it;
        function getReadyLine() {
            var rawData = sheetMAT.getRange(3, 2, sheetMAT.getLastRow() - 2, 5).getValues();
            for (var i = 0; i < rawData.length; i++) {
                if (rawData[i][3] === "A Ready Line" && rawData[i][4] === "ATY") {
                    temp1 = ' ' + data[i][0];
                    temp2.push(data[i][1], temp1);
                    dataReadyLine.push(temp2);
                    temp1 = '';
                    temp2 = [];
                }
            }
            return dataReadyLine;
        }

        //This is the client side code that should populate the innerHTML of the DIV template that is shown below;
        setInterval(function() {
            google.script.run.withSuccessHandler(function(data) {
                var temp3 = '';
                for (var i = 0; i < data.length; i++) {
                    temp3 = 'p' + data[i] + '/p';
                    document.getElementById("#item1").innerHTML = temp3;
                    temp3 = '';
                }
            }).getReadyLine();
        }, 10000);

        setInterval(function() {
            google.script.run.withSuccessHandler(function(data) {
                var temp3 = '';
                for (var i = 0; i < data.length; i++) {
                    temp3 += '<p>' + data[i].join() + '</p>';
                }
                document.getElementById("item1").innerHTML = temp3;
            }).getReadyLine();
        }, 1000);

        //This is the code that dynamically extracts data every second from the server and populates it into to DIV element.
        setInterval(function() {
            google.script.run.withSuccessHandler(function(data) {
                var temp3 = '';
                for (var i = 0; i < data.length; i++) {
                    temp3 += '<p>' + data[i].join() + '</p>';
                }
                document.getElementById("item1").innerHTML = temp3;
            }).getReadyLine();
        }, 1000);

        //This is the code that should calculate the total height of all the children elements of DIV element. But it is not working, not calculating.
        var outerHeight1 = 0;
        $("#item1").children().each(function() {
            outerHeight1 += $(this).outerHeight();
        });


        $(document).ready(function() {
            setInterval(function() {
                google.script.run.withSuccessHandler(function(data) {
                    var temp3 = '';
                    for (var i = 0; i < data.length; i++) {
                        temp3 += '<p>' + data[i] + '</p>';
                    }
                    document.getElementById("item1").innerHTML = temp3;
                }).getReadyLine();
                var outerHeight1 = 0;
                $("#item1").children().each(function() {
                    outerHeight1 += $(this).outerHeight(true);
                });
                console.log(outerHeight1);
                $("#item1").animate({
                    scrollTop: outerHeight1
                }, 8000);
                setTimeout(function() {
                    $("#item1").animate({
                        scrollTop: 0
                    }, 8000);
                }, 1000);
                setInterval(function() {
                    // 4000 - it will take 4 secound in total from the top of the page to the bottom
                    $("#item1").animate({
                        scrollTop: outerHeight1
                    }, 8000);
                    setTimeout(function() {
                        $("#item1").animate({
                            scrollTop: 0
                        }, 8000);
                    }, 8000);

                }, 1000);
            }, 1000);

            var outerHeight2 = 0;
            $("#item2").children().each(function() {
                outerHeight2 += $(this).outerHeight(true);
            })

            $("#item2").animate({
                scrollTop: outerHeight2
            }, 8000);
            setTimeout(function() {
                $("#item2").animate({
                    scrollTop: 0
                }, 8000);
            }, 8000);
            setInterval(function() {
                // 4000 - it will take 4 secound in total from the top of the page to the bottom
                $("#item2").animate({
                    scrollTop: outerHeight2
                }, 8000);
                setTimeout(function() {
                    $("#item2").animate({
                        scrollTop: 0
                    }, 8000);
                }, 8000);

            }, 8000);

        });

【讨论】:

  • 你让这种方式变得更加复杂,它必须是。 jQuery animate 函数有很多针对动画不同阶段的回调函数(动画开始时、动画完成时等),您可以找到有关 HERE 的更多详细信息。例如,不用setTimeout,您可以这样做:$("#item1").amimate({scrollTop: $("#item1").height()}, 4000, function(){$(this).animate({scrollTop: 0}, 4000)}))
  • 我有固定高度的“#item1”,因为它旁边和下面都有盒子。如果没有 setTimeout,我也无法让它反复向下和向上滚动。
猜你喜欢
  • 1970-01-01
  • 2010-12-21
  • 2010-11-04
  • 2016-12-28
  • 1970-01-01
  • 2012-06-14
  • 1970-01-01
  • 2017-07-28
  • 1970-01-01
相关资源
最近更新 更多