[英]JavaScript works locally but not when deployed to webserver
I'm deploying some code to a website and when I do the JavaScript does not run. 我正在将一些代码部署到网站上,但是当我运行JavaScript时,它无法运行。 I get the error: 我得到错误:
SCRIPT5007: The value of the property '$' is null or undefined, not a Function object. SCRIPT5007:属性“ $”的值为null或未定义,不是Function对象。
The code it is using is 它使用的代码是
@model MVCMasterDetail.DataAccess.cwwoLoadCurrentWorkOrderActualsResult[]
<script type="text/javascript">
$(document).ready(function () {
$(".grid-toggle").click(function () {
$(this).closest("table").find("tbody").show();
});
$(".hide-rows").click(function () {
$(this).closest("table").find("tbody").hide();
});
});
</script>
@Html.Partial("MasterDetailMasterPartial")
And what calls uses it is: 调用使用它的是:
<td colspan="4"> Details<br/><a href="javascript: void(0)" class="grid-toggle">Show- </a><a href="javascript: void(0)" class="hide-rows">Hide</a></td>
Any help is appreciated. 任何帮助表示赞赏。
Based off your comment in Ashley Ross's answer, it looks like your trying to add jQuery using a relative path. 根据您在Ashley Ross答案中的评论,看来您尝试使用相对路径添加jQuery。 it also looks like you're using MVC. 看起来您正在使用MVC。 In MVC, the way you want to reference a file in html is: 在MVC中,您要在html中引用文件的方式是:
<script src="@Url.Content("~/Scripts/jquery-1.7.1.js")"></script>
Also, it doesn't matter if your script tag goes in the head tag or not. 另外,脚本标签是否位于head标签中也没有关系。 The only difference it makes is loading. 唯一的区别是加载。 By putting it in the head tag, you're telling the browser to download the js file before it begins to load the body, which can be nice, but can also increase page load times. 通过将其放在head标签中,可以告诉浏览器在开始加载正文之前先下载js文件,这虽然不错,但也可能增加页面加载时间。 For faster page download, you actually want to put your script tags towards the bottom of your body tag, which defers downloading the js file until after the rest of the page has loaded. 为了更快地下载页面,您实际上希望将脚本标签放在body标签的底部,这将推迟下载js文件,直到页面其余部分加载完毕。 This results in faster page load times, but can cause some funky behaviors if you aren't anticipating it. 这样可以加快页面加载时间,但是如果您不期望这样做,可能会导致一些时髦的行为。
You need to include jQuery before any other scripts. 您需要在所有其他脚本之前包含jQuery。 It sounds like you have something like 听起来你有类似的东西
<script type="text/javascript" src="customScriptThatUsesjQuery.js"></script>
<script type="text/javascript" src="jquery-x.y.z.min.js"></script>
instead of 代替
<script type="text/javascript" src="jquery-x.y.z.min.js"></script>
<script type="text/javascript" src="customScriptThatUsesjQuery.js"></script>
Check that it appears in your HTML source before any other scripts that use it. 检查它是否出现在您的HTML源代码中,然后再使用它的其他脚本。
您必须在<head>
添加对jquery lib的引用
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.