物化导航栏下拉菜单不起作用

时间:2020-08-27 10:16:32

标签: javascript html

出于某种原因,我的导航栏中的下拉菜单无法正常工作。下拉按钮在那里,但是没有下拉,因此下拉触发部分可能有问题。我已经尝试了一切,但无法解决,感谢您的帮助。以下是实现文档的链接:https://materializecss.com/navbar.html

{% load static %}

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Casual Clothing comparer</title>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
    <link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
</head>

<body>
<!-- Dropdown Structure -->
<ul id="dropdown1" class="dropdown-content">
  <li><a href="#!">one</a></li>
  <li><a href="#!">two</a></li>
  <li class="divider"></li>
  <li><a href="#!">three</a></li>
</ul>
<nav>
  <div class="nav-wrapper">
    <a href="#!" class="brand-logo">Logo</a>
    <ul class="right hide-on-med-and-down">
      <li><a href="sass.html">Sass</a></li>
      <li><a href="badges.html">Components</a></li>
      <!-- Dropdown Trigger -->
      <li><a class="dropdown-trigger" href="#!" data-target="dropdown1">Dropdown<i class="material-icons right">arrow_drop_down</i></a></li>
    </ul>
  </div>
</nav>

<div class="row">
    {% for post in final_products %}
        <div class="col s12 m6 l4">
            <div class="card horizontal hoverable z-depth-1-half">
                <div class="card-image" id="card_image" style="height:300px">
                    <img src="{{ post.6 }}">
                </div>
                <div class="card-stacked">
                    <div class="card-content">
                        <span class="card-title">{{ post.0 }}</span>
                        <p>Initial Price €{{ post.3 }}</p>
                        <p>Discount {{ post.7 }}%</p>
                        <p style="margin-top: 10px; font-size: x-large">Price €{{ post.2 }}</p>
                    </div>
                    <div class="card-action">
                        <a href="{{ post.5 }}" style="color: green; font: bold">Buy</a>
                    </div>
                </div>
            </div>
        </div>
    {% endfor %}
</div>

<footer id="sticky-footer" class="py-4 bg-dark text-white-50">
    <div class="container text-center">
      <small>Copyright &copy; Your Website</small>
    </div>
</footer>

<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<script>
    $(document).ready(function(){
        //Initialize dropdown
        $(".dropdown-trigger").dropdown();
    });
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

$(document)是jQuery选择器,您没有包括jQuery的路径。 我将以下行添加到您的代码中,并且下拉列表工作正常:<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>。您可以了解有关此here的更多信息。

代码行应包含在以下内容之前:

<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>

代码无法正常工作的原因是Materialize CSS使用jQuery方法。