【问题标题】:Sometimes I need to refresh my page to make my jQuery script at footer work有时我需要刷新我的页面以使我的 jQuery 脚本在页脚工作
【发布时间】:2017-10-27 14:31:03
【问题描述】:

我有一个固定的标题,因为它是固定的它一些页面,所以我需要在first-page-content 中添加一个margin-top。我考虑过使用一个 jQuery 脚本来检查header 呈现的height 并将其作为margin-top 添加到first-page-content。它可以工作,但有时我需要刷新页面,这对用户来说相当麻烦。

脚本(在页脚处):

$(document).ready(function(){ // This function is activated after the document is loaded
  $('.first-page-content').css('margin-top', $('#header').height()); // Sets the margin-top of the first-page-content as the height of the header
});

In case you need the jQuery API documentation

编辑:当它不起作用时,标题一些.first-page-content,因为没有margin-top

【问题讨论】:

  • 首先,如果脚本在页脚中,则不需要准备好文档。其次,您没有说明“为什么”必须刷新。实际问题是什么。
  • @Taplar,谢谢,我会删除 $(document).ready() :]

标签: jquery css


【解决方案1】:

使用以下方法解决:

$(window).on('load', function(){
  $('.first-page-content').css('margin-top', $('#header').height()); // Sets the margin-top of the first-page-content as the height of the header
});

reference

【讨论】:

    【解决方案2】:

    阅读:

    setInterval(my_function,500);
    

    然后您的删除/创建边距的功能将始终在 0.5 秒后刷新。

    【讨论】:

      猜你喜欢
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      相关资源
      最近更新 更多