You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Material Design Bootstrap(MDB)NPM安装后报错求助:Cannot read property 'hasAttribute' of null

Fixing "Uncaught TypeError: Cannot read property 'hasAttribute' of null" in MDB UI Kit

Hey there, let's work through this MDB error you're hitting! That hasAttribute("data-mdb-no-jquery") related error usually pops up when MDB's JS can't find what it needs to handle jQuery dependencies correctly. Here are a few tried-and-true fixes to get you back on track:

1. Handle jQuery Dependencies Properly

Option A: Enable jQuery (if you need it)

MDB still relies on jQuery for some components in certain versions. Make sure jQuery is loaded before MDB's script, and exposed globally so MDB can pick it up:

// In your resources/js/app.js
import $ from 'jquery';
window.$ = window.jQuery = $; // Expose jQuery globally
import 'mdb-ui-kit/js/mdb.min.js';

Option B: Disable jQuery (use MDB's pure JS mode)

If you don't want to use jQuery, tell MDB explicitly by adding the data-mdb-no-jquery attribute to your root HTML element (either <html> or <body>):

<html data-mdb-no-jquery>
  <!-- Your page content here -->
</html>

This tells MDB to skip checking for jQuery entirely, which should resolve the null reference error.

2. Ensure MDB Initializes After the DOM is Ready

If MDB tries to run before your page's DOM is fully loaded, it might look for elements that don't exist yet. Wrap your MDB initialization code (or ensure your bundle loads) after the DOM is ready:

// For vanilla JS projects
document.addEventListener('DOMContentLoaded', () => {
  // Initialize MDB components here (e.g., new mdb.Dropdown(document.getElementById('my-dropdown')));
});

If you're using a framework like Vue or React, make sure you initialize MDB components in the mounted (Vue) or componentDidMount/useEffect (React) lifecycle hooks.

3. Clear Cache and Rebuild

Webpack or npm cache can sometimes cause stale code issues. Try a clean install and rebuild:

rm -rf node_modules package-lock.json
npm install
npm run build --no-cache

4. Verify Version Compatibility

Double-check that your MDB UI Kit version is compatible with your project's environment (e.g., webpack version, jQuery version if you're using it). MDB's newer versions have shifted toward pure JS, so if you're on an older version, you might need to adjust your setup to match its requirements.

Start with options 1A or 1B—those are the most common fixes for this specific error. Let me know if you need more help troubleshooting!

内容的提问来源于stack exchange,提问作者Miyonu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 16:37:48