【发布时间】:2018-04-02 12:32:00
【问题描述】:
我已将我的 html 文件分为 header.php、content.php 和 footer.php 我想在 hader 或页脚文件中加载 js 文件和 document.getready 函数。但我收到一条错误消息:未捕获的 ReferenceError: $ is not defined。
在我的 header.php 中,我在 head 部分加载以下脚本:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js" async></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.2.5/jquery.fancybox.js" async></script>`
在我的页脚结束后</body>-Tag 我打电话:
<script>
$(document).ready(function() {
if( !window.isCMS) {
// Group images by gallery using data-fancybox-group attributes
var galleryId = 1;
$('.editable-gallery').each( function() {
$(this).find('a').attr('data-fancybox', 'gallery-' + galleryId++);
});
$('.editable-gallery').each( function() {
$(this).find('img').attr('width', '200');
});
// Initialize Fancybox
$("[data-fancybox]").fancybox({
// Use the alt attribute for captions per http://fancyapps.com/fancybox/#useful
//beforeShow: function() {
// var alt = this.element.find('img').attr('alt');
//this.inner.find('img').attr('alt', alt);
//this.title = alt;
//}
});
}
});
</script>
在我的 content.php 文件中,我包含我的 header.php 和 footer.php,如下所示:
<?php include ("header.php"); ?>
<?php include ("footer.php"); ?>
我必须如何以及在哪里实现脚本文件和 document.getready 函数?
这是我在 content.php 中的画廊代码:
<div id="my-gallery" class="editable-gallery">
<a href="fotos/2018/rl/02022018/3.jpg"><img src="fotos/2018/rl/02022018/3.jpg" alt=""></a>
<a href="fotos/2018/rl/02022018/1.jpg"><img src="fotos/2018/rl/02022018/1.jpg" alt=""></a>
<a href="fotos/2018/rl/02022018/3.jpg"><img src="fotos/2018/rl/02022018/3.jpg" alt=""></a>
</div>
我必须添加 attr "data-fancybox" 和 "width" 才能使用 fancybox。
【问题讨论】:
-
您是否在包含 header.php 文件的行上方使用了
$的任何 jquery 代码? -
向我们展示将生成的(简化的)html 代码
-
那是因为您在脚本中使用了
async属性。 -
这是异步属性。现在可以了!