Why does jQuery mask say it's not a function?

梦想与她 提交于 2021-02-06 08:39:50

问题


When trying to use the jQuery mask() function, I get the following error in the console:

TypeError: $(...).mask is not a function

This is a sample of my code where this is happening:

<html>
<body>
<input type='text' id='phone' />
</body>

<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>

<script>
//alert($);
$(document).ready(function(){

        $("#phone").mask("(99) 9999-9999");
});
</script>

</html>

How do I fix this?


回答1:


Jquery mask is a plugin. You can directly add this line in your HTML if you want to use a CDN version:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.10/jquery.mask.js"></script>

Another way is to use a package manager like npm or bower. To accomplish that, follow the instructions in the README.md file of the project.




回答2:


Change this line from

$(document).ready(function(){

to

$(document).ready(function($){




回答3:


jQuery itself does not provide functionality for masking an input. You can use one of the plugins avaiable for it.



来源:https://stackoverflow.com/questions/28512909/why-does-jquery-mask-say-its-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!