【发布时间】:2014-12-14 11:34:02
【问题描述】:
我正在尝试使用 jquery .load("file") 函数加载“header.html”,将 css 应用于加载的元素(作品),然后将 JS 应用于加载的元素,滚动浮动菜单等功能,但这不起作用。这是我在头部导入文件的顺序:
<!-- Elements going to be loaded are using this font-awesome css
so they must be loaded before anything -->
<link rel="stylesheet" href="awesome/css/font-awesome.min.css" />
<!-- Then importing the jquery -->
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<!-- This is the point where i load the header.html -->
<script src="js/indexLoad.js"></script>
<!-- Then apply css to the loaded elements (works) -->
<link rel="stylesheet" href="css/index.css" />
<!-- Then apply JS to the loaded elements (doesnt work) -->
<script src="js/index.js"></script>
在 header.html 中,我导入了一些元素,例如 nav。
然后将样式更改为nav 以将其显示为红色。
当我在index.js 中使用var newHeight = $("nav").position().top 时,我只得到cannot read property value of null... 为什么css 将样式应用于加载的元素而JS 却无法获取它们?
我在两个 .js 文件中都正确使用了$(document).ready( function(){}:
indexLoad.js
$(document).ready( function(){
$("header").load("http://localhost/js/header.html");
});
index.js
$(document).ready( function(){
var newHeight = $("nav").position().top;
});
【问题讨论】:
标签: javascript jquery html css