Uncaught TypeError: Cannot read property 'setAttribute' of undefined at Object.onLoad

Uncaught TypeError: Cannot read property 'setAttribute' of undefined at Object.onLoad

知道可能导致此错误的原因吗?

我的列表:

<link rel="stylesheet" href="../../node_modules/semantic-ui/dist/semantic.min.css">
<link rel="stylesheet" href="../../node_modules/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="../../node_modules/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="../../node_modules/fullcalendar/dist/fullcalendar.min.css">
<script src="../../node_modules/semantic-ui/dist/semantic.min.js"></script>
<script src="../../node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="../../node_modules/moment/min/moment.min.js"></script>
<script src="../../node_modules/fullcalendar/dist/fullcalendar.min.js"></script>

HTML 元素:

<div class="ui floating dropdown labeled search icon button" style="width: 95%; margin: 0 auto;" id="monthDrop">
    <i class="calendar icon"></i>
    <span class="text">Choose a Month</span>
    <div class="menu">
                <div class="item">January</div>
                <div class="item">February</div>
                <div class="item">March</div>
                <div class="item">April</div>
                <div class="item">May</div>
                <div class="item">June</div>
                <div class="item">July</div>
                <div class="item">August</div>
                <div class="item">September</div>
                <div class="item">October</div>
                <div class="item">November</div>
                <div class="item">December</div>
    </div>
</div>

脚本:

$('#monthDrop').dropdown();

它渲染良好,一切都很好,加载时没有错误,只是当我尝试点击它时:

CSS Frameworks

首先你必须选择你是使用 Bootstrap 4 还是 Semantic-UI,因为这是两个不同的 CSS 框架,同时使用它们是一团糟。

Bootstrap 4

假设您选择 Bootstrap 4,因为它更简单易学,特别是对于初学者(当然您可以选择 Semantic-UI、Foundation 或其他任何您愿意的)你的代码中应该有这两个脚本:jQueryPopper.js.

来自 Bootstraps Documentation:

Many of our components require the use of JavaScript to function. Specifically, they require jQuery, Popper.js, and our own JavaScript plugins.


Dropdown

同样,您可以在文档中找到:

Dropdowns are built on a third party library, Popper.js, which provides dynamic positioning and viewport detection. Be sure to include popper.min.js before Bootstrap’s JavaScript or use bootstrap.bundle.min.js / bootstrap.bundle.js which contains Popper.js. Popper.js isn’t used to position dropdowns in navbars though as dynamic positioning isn’t required.

当您决定要使用哪个 CSS 框架时,您将能够以适当的方式设置您的 Dropdowns。为了有更好的观点,您还应该研究 Semantic-UI Documentation about Dropdown.


NodeJS 环境 != 浏览器 JavaScript 环境

正如我所看到的,您是通过 npm 安装脚本的,但我现在确定这是否是您有意为之的。在 shorthand:

npm is a package manager for Node.js packages

我猜你想做的是将这些包的简单版本放在本地文件夹中,例如 ./project_name/javascript/bootstrap.js./project_name/css/bootstrap.min.css,现在不需要你有 node_modules。但是,再一次,如果你愿意,你当然可以这样拥有它。

您可以找到很多关于 Node 和 JavaScript here 的有用评论。

我们 运行 陷入了同样的错误 html:

<select class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" role="button" v-model="selected" aria-haspopup="true" aria-expanded="false">
    <option disabled value="">Please select one</option>
    <option class="dropdown-item" value="type1">Carrier</option>
    <option class="dropdown-item" value="type2">Shipper</option>
</select>

我们尝试从 <select> 标签中删除 data-toggle="dropdown";错误不再发生,下拉菜单仍然有效。不确定 为什么 这行得通,但它为我们消除了错误。一定是某种冲突?无论如何,如果其他人正在寻找解决方案,此解决方法可能对他们有用。

将语义 UI link 保持在 bootstrap 下方

<link rel="stylesheet" href="../../node_modules/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="../../node_modules/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="../../node_modules/semantic-ui/dist/semantic.min.css">
<link rel="stylesheet" href="../../node_modules/fullcalendar/dist/fullcalendar.min.css">

删除 data-toggle="dropdown" 对我不起作用,因为 Bootstrap 4.x 需要下拉菜单。

但是在类似的问题中,我认为我使用的第三方插件有一个名为 removeAttr 的已删除函数。当我用道具改变它时,我工作了。没有更多的错误。

我在 Bootstrap 4.x 中遇到了同样的错误,因为我的下拉菜单与我使用的下拉按钮没有相同的父级。

<span class="project-sort-by">Sort by: <a class="dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Recent</a></span>

<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
</div>

这不起作用,因为 dropdown.js 代码使用以下代码查找菜单:

  _getMenuElement() {
    if (!this._menu) {
      const parent = Dropdown._getParentFromElement(this._element)

      if (parent) {
        this._menu = parent.querySelector(SELECTOR_MENU)
      }
    }
    return this._menu
  }

要解决此问题,请移动菜单,使其与切换按钮具有相同的父项。

<span class="project-sort-by">Sort by: 
    <a class="dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Recent</a>
    <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
        <a class="dropdown-item" href="#">Action</a>
        <a class="dropdown-item" href="#">Another action</a>
        <a class="dropdown-item" href="#">Something else here</a>
    </div>
</span>

错误发生是因为您放错了元素。 None 上面的答案为我解决了。

位置错误 1

<span class="project-sort-by">Sort by: <a class="dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Recent</a></span>

<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
</div>

位置错误 2

<span class="project-sort-by">Sort by: 
    <a class="dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Recent</a>
    <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
        <a class="dropdown-item" href="#">Action</a>
        <a class="dropdown-item" href="#">Another action</a>
        <a class="dropdown-item" href="#">Something else here</a>
    </div>
</span>

正确的位置

<span class="dropdown project-sort-by">Sort by: 
    <a class="dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Recent</a>
    <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
        <a class="dropdown-item" href="#">Action</a>
        <a class="dropdown-item" href="#">Another action</a>
        <a class="dropdown-item" href="#">Something else here</a>
    </div>
</span>