Javascript 为什么 jQuery 掩码说它不是一个函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28512909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:54:51  来源:igfitidea点击:

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

javascriptjqueryjquery-mask

提问by Hyman

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

尝试使用 jQuerymask()函数时,我在控制台中收到以下错误:

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?

我该如何解决?

回答by Gnucki

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

Jquery 掩码是一个插件。如果要使用 CDN 版本,可以直接在 HTML 中添加此行:

<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.mdfile of the project.

另一种方法是使用像 npm 或 bower 这样的包管理器。为此,请按照项目的README.md文件中的说明进行操作。

回答by RAHUL KRISHNA

Change this line from

将这一行从

$(document).ready(function(){

$(document).ready(function(){

to

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

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

回答by Sigismundus

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

jQuery 本身不提供屏蔽输入的功能。您可以使用可用的插件之一。