Javascript 类型错误:$(...)。不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37476912/
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
TypeError: $(...). is not a function
提问by Rakyir
I'm trying to track down an error TypeError: $(...).clientSideCaptcha is not a function at RegisterCapcha (http://localhost:4382/xxx/index.html:98:31)
我试图追查一个错误 TypeError: $(...).clientSideCaptcha is not a function at RegisterCapcha ( http://localhost:4382/xxx/index.html:98:31)
, am working in angularJS project. simple issue but i can't got out from it.
,我在 angularJS 项目中工作。简单的问题,但我无法摆脱它。
i have implemented a captcha using j query and successfully working in a view when on on-click but thing is i have try to load that same captcha in other view in on-load but captcha function is not loading.
我已经使用 j 查询实现了验证码,并在单击时成功在视图中工作,但问题是我尝试在加载时在其他视图中加载相同的验证码,但验证码功能未加载。
in view1 captcha working fine code below
在下面的 view1 验证码中工作正常
view1.html
视图1.html
<a onclick="RegisterCapcha(); ReverseContentDisplay('job-apply'); return true;"
href="javascript:ReverseContentDisplay('job-apply')">
<form id="careersForm" >
<table class="apply-form">
<tr>
<td>First Name </td>
<td><input type="text" name="First_Name" class="applytext" required id="First_Name"></td>
</tr>
<tr>
<td colspan="2" class="applytable" >
<div class="editor-field">
<p>
<label>
Enter the text shown below:
<input type="text" id="captchaText" onkeyup="javascript:EnableApply();" /></label></p>
<p id="captcha">
</p>
</div>
</td>
</tr>
<tr>
<input type="submit" id="careerbtn" name="button" disabled="disabled" class="send-resume" value="SEND" style="margin-left:24%;">
script declared in index.html
在 index.html 中声明的脚本
<script src="js/captcha/jquery.clientsidecaptcha.js" type="text/javascript"></script>
<script type="text/javascript">
function EnableApply() {
var OriginalCaptcha = $('#careersForm').data('captchaText');
var userCapcha = $('#captchaText').val();
if (OriginalCaptcha == userCapcha) {
$('#careerbtn').removeAttr('disabled');
}
else {
$('#careerbtn').attr('disabled', 'disabled');
}
}
function RegisterCapcha() {
$("#captcha").html(''); //reset the generated captcha first
$("#captchaText").val('');
$("#careersForm").clientSideCaptcha({
input: "#captchaText",
display: "#captcha",
});
}
</script>
view2:same form with same RegisterCapcha(); script in onload but calling in view2
view2:相同的形式与相同的 RegisterCapcha(); 脚本在 onload 但在 view2 中调用
<script type="text/javascript">
$(document).ready(function () { RegisterCapcha(); });
</script>
<form id="careersForm" >
<table class="apply-form">
<tr>
<td>First Name </td>
<td><input type="text" name="First_Name" class="applytext" required id="First_Name"></td>
</tr>
<tr>
<td colspan="2" class="applytable" >
<div class="editor-field">
<p>
<label>
Enter the text shown below:
<input type="text" id="captchaText" onkeyup="javascript:EnableApply();" /></label></p>
<p id="captcha">
</p>
</div>
</td>
</tr>
<tr>
<input type="submit" id="careerbtn" name="button" disabled="disabled" class="send-resume" value="SEND" style="margin-left:24%;">
please help me to solve it thanks in advance :)
请帮我解决它提前谢谢:)
采纳答案by Ivijan Stefan Stipi?
1) Include jQuery before all your scripts:
1) 在所有脚本之前包含 jQuery:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="js/captcha/jquery.clientsidecaptcha.js" type="text/javascript"></script>
2) After that define $
variable for jQuery function like this:
2)之后$
为jQuery函数定义变量,如下所示:
<script type="text/javascript">
(function($){
function EnableApply() {
var OriginalCaptcha = $('#careersForm').data('captchaText');
var userCapcha = $('#captchaText').val();
if (OriginalCaptcha == userCapcha) {
$('#careerbtn').removeAttr('disabled');
}
else {
$('#careerbtn').attr('disabled', 'disabled');
}
}
function RegisterCapcha() {
$("#captcha").html(''); //reset the generated captcha first
$("#captchaText").val('');
$("#careersForm").clientSideCaptcha({
input: "#captchaText",
display: "#captcha",
});
}
}(jQuery));
</script>
回答by Ann
After loading jQuery
加载 jQuery 后
<script src='jquery.js'></script>
if (typeof $ == 'undefined') {
var $ = jQuery;
}
Reference:
参考:
回答by Kuldeep Kumawat
please include latest jquery before all the script tags. find latest jquery at https://code.jquery.com/or use cdn
请在所有脚本标签之前包含最新的 jquery。在https://code.jquery.com/ 上找到最新的 jquery或使用 cdn
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
回答by Alexis
For use Jquery plug-in (library) you need to import Jquery first.
要使用 Jquery 插件(库),您需要先导入 Jquery。
The better way is import all scripts at the end of your HTML.
更好的方法是在 HTML 的末尾导入所有脚本。
Take care of the order(Jquery first).
注意顺序(首先是Jquery)。
Example of Jquery import (online version).
Jquery 导入示例(在线版)。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
For some reason you don't want import script at the end, don't forget :
出于某种原因,您不想在最后导入脚本,请不要忘记:
$(document).ready(function(){//code});
Some plug-in need HTML to be loaded before use them. So the script begins to end.
有些插件需要加载 HTML 才能使用。于是剧本开始结束。