简体   繁体   English

初始化物化函数

[英]Initializing Materialize functions

I am having trouble initializing some Materialize-css javascript functions;我在初始化某些 Materialize-css javascript 函数时遇到问题; the one in particular is material_select().特别是material_select()。

I am trying to initialize the function as stated in the Materialize docs:我正在尝试按照 Materialize 文档中的说明初始化函数:

$(document).ready(function() { $('select').material_select(); });

However, I get the following error: $(...).material_select is not a function但是,我收到以下错误: $(...).material_select is not a function

material_select() as well as any other Materialize function initializes if I manually write it into the browser console, but not if it's in the code;如果我手动将material_select()以及任何其他 Materialize 函数初始化到浏览器控制台,但如果它在代码中则不会; I am importing jQuery before materialize.js as well.我也在 materialize.js 之前导入 jQuery。

Help is much appreciated, thank you.非常感谢您的帮助,谢谢。

You are most likely loading materialize.js before jQuery or your jQuery version is too high and not working with materialize.您很可能在 jQuery 之前加载 materialize.js,或者您的 jQuery 版本太高而无法使用 materialize。 You need to add some code here!你需要在这里添加一些代码!

Double check that jQuery and Materialize are initialized before the document ready function is called.在调用文档就绪函数之前,仔细检查 jQuery 和 Materialize 是否已初始化。

For example:例如:

<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="js/materialize.min.js"></script>
<script>
$(document).ready(function() {
    $('select').material_select();
});
</script>

And of course make sure that the appropriate CSS files are imported in <head>当然还要确保在<head>中导入适当的 CSS 文件

It is impossible to give the exact solution without referring your code.不参考您的代码就不可能给出确切的解决方案。 Problem may be happened in the materialize.js file.问题可能发生在materialize.js文件中。 It would be better if you test following fully working code and find your issue.如果您测试以下完全工作的代码并找到您的问题会更好。

Working code工作代码

Click Run Code Snippet in the given link for live demo or copy full code and test in your machine.单击给定链接中的Run Code Snippet以进行实时演示或复制完整代码并在您的机器中进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM