您的位置:首頁技術文章
文章詳情頁

javascript - requirejs加載報錯問題?

瀏覽:99日期:2023-02-09 16:21:28

問題描述

1、使用requirejs寫了個demo.html,第一次加載頁面時能夠正常顯示,隨后刷新頁面就開始報錯,雖然頁面還是正常顯示,但是我不理解為什么會這樣。2、我的另一個999.html文件(跟demo.html在同級目錄下,但是頁面內容比較多,其余和1中的html沒有太大區別)一直報錯,要么顯示require is not define要么顯示Script error for 'jquery'或$ is not defined,請問這是為什么,應該怎么改?

以下是文件目錄:javascript - requirejs加載報錯問題?

代碼如下:

demo.html:

<!DOCTYPE html><html> <head><meta charset='UTF-8'><title></title><script src='http://www.wxshucaidpc.com/wenda/resource/require.js'></script><script src='http://www.wxshucaidpc.com/wenda/resource/main.js'></script> </head> <body><script type='text/javascript'> require([’jquery’,’weui’],function($){$(’body’).css(’background’,’red’); });</script> </body></html>

main.js:

require.config({ baseUrl:’resource/app’, paths:{’jquery’:’../lib/jquery-3.2.0.min’,’zepto’:’../lib/zepto.min’,’css’:’../lib/css.min’,’weui’:’../lib/jquery-weui.min’,’cookie’:’../lib/jquery.cookie’,’swiper’:’../lib/swiper’ }});

報錯信息:javascript - requirejs加載報錯問題?

999.html:

<!DOCTYPE html>

<html>

<head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no' /> <title></title> <link rel='stylesheet' type='text/css' href='http://www.wxshucaidpc.com/wenda/resource/css/weui.css' /> <link rel='stylesheet' type='text/css' href='http://www.wxshucaidpc.com/wenda/resource/css/jquery-weui.min.css' /> <link rel='stylesheet' type='text/css' href='http://www.wxshucaidpc.com/wenda/resource/css/index.css' /> <link rel='stylesheet' type='text/css' href='http://www.wxshucaidpc.com/wenda/resource/css/login.css'/> <link rel='shortcut icon' href='http://www.wxshucaidpc.com/wenda/resource/font/cake.ico' /> <script src='http://www.wxshucaidpc.com/wenda/resource/require.js' data-main = 'resource/main' defer async='true'></script></head><body ontouchstart> <!--login section--> <p style='height: auto;'><p class='beforeLogin'> <header><p class='head-bg'></p><p class='touxiang'> <img src='http://www.wxshucaidpc.com/wenda/img/head/0.png' /></p> </header> <!--登錄--> <section id='login'><form action='login.php' method='post' id='loginForm'> <input type='number' value='' placeholder='請輸入手機號' name='user' maxlength='11' /> <input type='password' value='' placeholder='請輸入密碼(6-16位)' name='psw' maxlength='16' /> <button type='submit' id='loginBut'>登錄</button></form><span>沒有賬號?<a href='http://www.wxshucaidpc.com/wenda/4035.html#regis'>注冊</a></span><br /><span><a href='http://www.wxshucaidpc.com/wenda/4035.html#forget'>忘記密碼?</a></span> </section> <!--注冊--> <section id='regis'><form id='checkphone'> <input type='hidden' name='forwhat' value='forcheck' /> <input type='number' value='' placeholder='請輸入手機號' name='user' maxlength='11' /> <button type='button' style='color: #fff;background: orange;'>點擊發送<br />驗證碼</button></form><form action='register.php' method='post' id='regisForm'> <input type='hidden' name='forwhat' value='forrig' /> <input type='number' value='' placeholder='請輸入驗證碼' name='validate' /> <input type='password' value='' placeholder='設置密碼(6-16位)' name='psw' maxlength='16' /> <input type='password' value='' placeholder='重新輸入密碼' name='pswcheck' /> <button type='submit' id='regisBut'>注冊</button></form><span>已有賬號?<a href='http://www.wxshucaidpc.com/wenda/4035.html#login'>登錄</a></span><br /><span><a href='http://www.wxshucaidpc.com/wenda/4035.html#forget'>忘記密碼?</a></span> </section> <!--忘記密碼--> <section id='forget'><form id='check4forget'> <input type='hidden' name='forwhat' value='forcheck' /> <input type='number' name='findphone' value='' placeholder='請輸入注冊的手機號' maxlength='11'/> <button type='button' id='but2'>點擊發送<br />驗證碼</button></form><form action='validatePhone.php' method='post' id='forgetForm'> <input type='hidden' name='forwhat' value='forreset' /> <input type='number' value='' placeholder='請輸入驗證碼' name='validate' /> <input type='password' value='' placeholder='重新設置密碼(6-16位)' name='psw-forget' maxlength='16' /> <input type='password' value='' placeholder='重新輸入密碼' maxlength='16'/> <button type='submit' id='reset'>重置密碼</button></form><span>已有賬號?<a href='http://www.wxshucaidpc.com/wenda/4035.html#login'>登錄</a></span><br /><span>沒有賬號?<a href='http://www.wxshucaidpc.com/wenda/4035.html#regis'>注冊</a></span> </section></p><p class='alreadyLogin'> <header><p class='head-bg'> <h3></h3></p><p class='touxiang'> <img src='http://www.wxshucaidpc.com/wenda/4035.html' /></p> </header> <section><p style='margin-top: 60px;'> <a href='http://www.wxshucaidpc.com/wenda/4035.html#' class='weui-cell weui-cell_access'><p class='weui-cell__bd'> <p>我的收藏</p></p> </a> <a href='javascript:;' class='weui-cell weui-cell_access kt-exit'><p class='weui-cell__bd'> <p style='color:red;'>退出登錄</p></p> </a></p> </section></p> </p><script type='text/javascript'> require([’jquery’,’weui’],function($){ });</script></body></html>

報錯信息:javascript - requirejs加載報錯問題?

問題解答

回答1:

手動聲明依賴

require.config({ baseUrl:’resource/app’, paths:{’jquery’:’../lib/jquery-3.2.0.min’,’zepto’:’../lib/zepto.min’,’css’:’../lib/css.min’,’weui’:’../lib/jquery-weui.min’,’cookie’:’../lib/jquery.cookie’,’swiper’:’../lib/swiper’ }, shim: {weui: {deps: ['jquery']}}});

以及 999.html 中加載 require.js 時不要 defer 和 async

標簽: JavaScript
相關文章:
国产综合久久一区二区三区