页面加载Spinner报错Uncaught TypeError: $ is not function的解决方法
解决"Uncaught TypeError: $ is not a function"错误并完成加载Spinner需求
一、错误原因及解决
1. 错误根源
你代码里用了$(jQuery简写)但未引入jQuery库,同时还存在DOM选择器错误:JS里选择的是#loader,但HTML中Spinner的外层容器ID是#loading——即使引入jQuery也找不到目标元素。
2. 两种解决方式
方式1:用原生JavaScript替代jQuery(推荐,无额外依赖)
直接改用原生DOM操作,无需引入任何外部库:
window.addEventListener('load', function () { // 找到loading容器并隐藏 document.getElementById('loading').style.display = 'none'; });
方式2:引入本地jQuery库
如果坚持用jQuery,需在自定义JS代码前引入本地的jQuery文件(禁止外链,路径根据你的文件位置调整):
<script src="jquery.min.js"></script> <script> window.addEventListener('load', function () { // 修正选择器为#loading $('#loading').hide(); }); </script>
二、完整需求实现代码
以下是整合所有需求的完整代码,采用原生JS实现:
完整HTML(新增触发按钮)
<div id="loading"> <div class="loader"> <div class="inner one"></div> <div class="inner two"></div> <div class="inner three"></div> </div> </div> <!-- 触发JSON处理的按钮 --> <button id="processBtn">处理JSON数据</button>
完整CSS(保留原有样式)
.loader { position: absolute; top: calc(50% - 32px); left: calc(50% - 32px); width: 64px; height: 64px; border-radius: 50%; perspective: 800px; } .inner { position: absolute; box-sizing: border-box; width: 100%; height: 100%; border-radius: 50%; } .inner.one { left: 0%; top: 0%; animation: rotate-one 1s linear infinite; border-bottom: 3px solid green; } .inner.two { right: 0%; top: 0%; animation: rotate-two 1s linear infinite; border-right: 3px solid green; } .inner.three { right: 0%; bottom: 0%; animation: rotate-three 1s linear infinite; border-top: 3px solid green; } @keyframes rotate-one { 0% { transform: rotateX(35deg) rotateY(-45deg) rotateZ(0deg); } 100% { transform: rotateX(35deg) rotateY(-45deg) rotateZ(360deg); } } @keyframes rotate-two { 0% { transform: rotateX(50deg) rotateY(10deg) rotateZ(0deg); } 100% { transform: rotateX(50deg) rotateY(10deg) rotateZ(360deg); } } @keyframes rotate-three { 0% { transform: rotateX(35deg) rotateY(55deg) rotateZ(0deg); } 100% { transform: rotateX(35deg) rotateY(55deg) rotateZ(360deg); } } #loading { width: 100%; height: 100%; top: 0; left: 0; position: fixed; display: block; opacity: 0.7; background-color: #fff; z-index: 99; text-align: center; } #loading-image { position: absolute; top: 100px; left: 240px; z-index: 100; }
完整JavaScript(实现所有需求)
const loadingElement = document.getElementById('loading'); const processBtn = document.getElementById('processBtn'); // 页面加载完成后隐藏Spinner window.addEventListener('load', function () { loadingElement.style.display = 'none'; }); // 模拟JSON处理函数(替换为你的实际逻辑) function processJSONData() { return new Promise((resolve) => { setTimeout(() => { resolve({ data: '模拟JSON处理结果' }); }, 2000); }); } // 点击按钮触发Spinner显示及JSON处理 processBtn.addEventListener('click', async function () { // 显示Spinner loadingElement.style.display = 'block'; try { // 执行JSON处理 const result = await processJSONData(); console.log('JSON处理完成:', result); } catch (error) { console.error('JSON处理出错:', error); } finally { // 无论成功失败,处理完成后隐藏Spinner loadingElement.style.display = 'none'; } });
三、关键逻辑说明
- 页面加载隐藏:通过
window.load事件确保所有资源加载完成后再隐藏Spinner - 点击按钮显示:绑定按钮点击事件,将
#loading的display设为block - JSON处理完成隐藏:用
async/await处理异步操作,在finally块中隐藏Spinner,保证无论处理成功或失败都会执行隐藏操作
内容的提问来源于stack exchange,提问作者Berk Birkan
相关产品推荐
相关产品推荐

