[英]jquery method in another file not found
I have a file- "Site.js" with the following method: 我有一个文件“ Site.js”,使用以下方法:
function Init() {
$(document).ready(function() {
//some init actions here
});
}
function jQuery.fn.DivToggle(div) {
$(div).toggle('fast');
//some other animation code here
}
in my Index.cshtml file (I'm using asp.net mvc), I have this: 在我的Index.cshtml文件中(我正在使用asp.net mvc),我有以下内容:
<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/Site.js")" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
$(function () {
Init();
});
</script>
</body>
</html>
I get this error when I run the project: "Microsoft JScript runtime error: 'Init' is undefined" 运行项目时收到此错误:“ Microsoft JScript运行时错误:'Init'未定义”
Any idea what I'm doing wrong? 知道我在做什么错吗?
Init
is not defined because there is a syntax error in Site.js
. 未定义Init
因为Site.js
存在语法错误。
function jQuery.fn.DivToggle(div) {
$(div).toggle('fast');
//some other animation code here
}
You can't actually do that. 您实际上无法做到这一点。 It should be: 它应该是:
jQuery.fn.DivToggle = function(div) {
$(div).toggle('fast');
//some other animation code here
};
PS $(function(){
is the same as $(document).ready(function(){
. You don't need the $(document).ready(function(){
inside your Init
function. PS $(function(){
与$(document).ready(function(){
$(function(){
$(document).ready(function(){
。您不需要在Init
函数内部使用$(document).ready(function(){
。
I think the real problem is with your DivToggle() function. 我认为真正的问题在于您的DivToggle()函数。 You should change that code to the following: 您应该将该代码更改为以下内容:
(function( $ ) {
$.fn.DivToggle = function() {
$(this).toggle('fast');
};
})( jQuery );
Then you can call DivToggle by creating a selector and calling it. 然后,您可以通过创建选择器并调用它来调用DivToggle。 So if you want to run this on all of the div tags in your page do this: 因此,如果要在页面中的所有div标签上运行此代码,请执行以下操作:
$('div').DivToggle();
Here is some documentation on creating custom plugins: http://docs.jquery.com/Plugins/Authoring 以下是有关创建自定义插件的一些文档: http : //docs.jquery.com/Plugins/Authoring
You probably need to change the way you are calling Init() and document.ready(). 您可能需要更改调用Init()和document.ready()的方式。 Actually the Init() function isn't really doing anything right now. 实际上,Init()函数现在实际上并没有做任何事情。 I think you can change your code to this and it would accomplish what you want: 我认为您可以将代码更改为此,它将完成您想要的:
<script type="text/javascript">
//create the DivToggle() function
(function( $ ) {
$.fn.DivToggle = function() {
$(this).toggle('fast');
};
})( jQuery );
$(function () {
//call DivToggle() on all <div> tags when the page loads
$('div').DivToggle();
});
</script>
Try namespacing your javascript: 尝试命名您的javascript:
StackOverflow = {
function Init(){
}
return {
Init: Init
};
}();
Call your function with 调用您的函数
$(function(){
StackOverflow.Init();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.