【问题标题】:Get height of div once javascript dynamic content is loaded into it将 javascript 动态内容加载到其中后获取 div 的高度
【发布时间】:2011-12-31 11:11:54
【问题描述】:

我刚刚意识到我在最后一个问题上搞砸了。

我正在使用下面的脚本来获取 DIV#tweet-area 上的动态高度并将该高度插入 CSS。

    var tweetheight = $("#tweet-area").height();
    $("#sidebar-wrapper").css({ bottom: tweetheight});

它很漂亮,但我刚刚意识到它的高度是加载我的 twitter 内容之前 div 的高度。我的 twitter 内容是由一个 jQuery 插件生成的,见下文。

我正在使用这个 jQuery 推特插件 - tweet.seaofclouds.com/jquery.tweet.js

并像这样加载脚本 -

    $("#latest-tweet").tweet({
        count: 1,
        username: ["motocomdigital"],
        loading_text: "searching twitter...",
    });

我正在将推文加载到位于 DIV#tweet-area 内的 DIV#latest-tweet 中。见下文

    <div id="tweet-area" class="clearfix">
        <div id="latest-tweet"></div>   
    </div>

我尝试在 twitter 脚本之后添加我的高度脚本,但仍然得到相同的高度:/

    <script>
        $(document).ready(function() {

                $("#latest-tweet").tweet({
                        count: 1,
                        username: ["motocomdigital"],
                        loading_text: "searching twitter..."
                });

                var tweetheight = $("#tweet-area").height();
                $("#sidebar-wrapper").css({ bottom: tweetheight});

        });
    </script>

任何帮助都会很棒,因为这对我来说是个小专家。非常感谢

【问题讨论】:

    标签: jquery dom html height var


    【解决方案1】:

    尝试以下方式:

        <script>
            $(document).ready(function() {
    
                    $("#latest-tweet").tweet({
                            count: 1,
                            username: ["motocomdigital"],
                            loading_text: "searching twitter..."
                    }).bind("loaded", function(){
                            tweetheight = $("#tweet-area").height();
                            $("#sidebar-wrapper").css({ bottom: tweetheight});
                    });
            });
        </script>
    

    这在你得到的插件中实现了一个记录不充分的调用者,它是在代码运行后对loaded处理程序的调用。祝你好运!

    【讨论】:

    • 完美的家伙,非常感谢! - 感谢您查看,我不会找到那个处理程序 - 虽然我以前从未使用过 .bind 但现在肯定会使用。
    【解决方案2】:

    seaofclouds 插件没有回调,而是有一个loaded 事件,该事件在推文加载完成时触发。

    因此,以下内容应该适合您:

    $("#latest-tweet").tweet({
        count: 1,
        username: ["motocomdigital"],
        loading_text: "searching twitter..."
    })
    .bind("loaded", function() {
        var tweetheight = $("#tweet-area").height();
        $("#sidebar-wrapper").css({ bottom: tweetheight});
    });                
    

    【讨论】:

      【解决方案3】:

      在线229您阅读的插件

      $(widget).trigger("loaded")
      

      这会在您绑定推文的 div 上触发事件 loaded。所以你可以这样做

      $("#latest-tweet").tweet({    
          // options
      }).bind("loaded", function(){
          // calc height
      });
      

      【讨论】:

      • 是的,刚刚查看了插件的内部 - 感谢您向我解释这一点,帮了很多忙!
      • 你好,我知道这有点厚颜无耻,但是你之前对我的问题很熟悉,你能看看这个http://goo.gl/NWNx8 - 如果你能提供帮助,可以通过贝宝捐款:)
      • 嗨试试这个重写问题stackoverflow.com/questions/8176034/… - 谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 2016-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多