問題描述
所以我最近從 ThemeForest 購買并下載了一個模板.我在本地服務器上設置了模板.我使用 codeigniter 生成了具有下拉菜單的導航.我將菜單中的所有內容都正確編碼.我只生成了標題并進行了測試,并意識到下拉菜單不起作用.我花了大約兩個小時試圖找出我的代碼出了什么問題.然后我終于意識到 JavaScript 文件包含在模板的頁腳中......我還沒有生成.
So I recently bought and downloaded a template from ThemeForest. I set up the template on my local server. I generated the navigation, which has dropdown menus, with codeigniter. I had everything in the menu coded correctly. I only had the header generated and went to test it out and realized the dropdown menus were not working. I spent about two hours trying to figure out what was wrong with my code. Then I finally realized that the JavaScript files were included in the footer of the template... which I had not yet generated.
所以我的問題是……為什么有人會將 JavaScript 文件放在 HTML 頁面的頁腳中?我認為 JS 只應該位于文件的標題中.對吧?
So my question is... why would someone put JavaScript files in the footer of an HTML page? I thought that JS was only supposed to be located in the header of the file. Right?
推薦答案
您可以通過多種方式將腳本文件加載到網頁.
There are different ways you could load a script file to a web page.
但是在頁面底部加載腳本有以下優點.
But loading scripts at bottom of you page have the following advantages.
- 不會阻止任何要加載的 DOM 內容.
- 所有 DOM 元素都可用于正在加載的腳本.
- 如果您使用第三方庫,例如 jQuery,則可以跳過
$(document).ready(function () {...});
,因為 DOM 已經加載.
- Will not block any DOM content to be loaded.
- All the DOM elements will be available for the scripts that are being loaded.
- If you are using third-party libraries such as jQuery, you can skip the
$(document).ready(function () {...});
as the DOM is already loaded.
希望這會有所幫助.
這篇關于為什么將 JavaScript 放在頁面的頁腳中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!