简体   繁体   English

MVC忽略了我的Javascript文件。

[英]MVC is ignoring my Javascript file.

A while ago I created an MVC application before I really understood that MVC is more backend than frontend (as in, I wasn't entirely sure where my custom html fit). 前一阵子我创建了一个MVC应用程序,然后才真正了解MVC比后端更前端(例如,我不确定自己的html适合什么位置)。 So I created a website that was just an HTML page, a CSS file and a Javascript file. 因此,我创建了一个仅包含HTML页面,CSS文件和Javascript文件的网站。 It worked beautifully so I returned to my MVC application to map what I created onto it so I could serve up information from the backend and not clutter my html with paragraphs of information. 它工作得很漂亮,所以我回到了我的MVC应用程序中,将我创建的内容映射到它上,这样我就可以从后端提供信息,而不会使html的信息段落混乱。 I put the JS file in the Scripts folder and included it in my index.cshtml. 我将JS文件放在Scripts文件夹中,并将其包含在index.cshtml中。

Upon debugging, I found that it wasn't that the JS was not working, it wasn't included in the project at all. 调试后,我发现不是JS无法正常工作,它根本没有包含在项目中。 This in both Firefox Developer Edition and Internet Explorer. 在Firefox Developer Edition和Internet Explorer中都可以使用。

I tried adding reference to it in reference.js. 我尝试在reference.js中添加对它的引用。 I tried creating a .DLL and calling it into my page. 我尝试创建一个.DLL并将其调用到我的页面中。 I've tried deleting all of the other Javascript files to make sure that it wasn't being overridden, only to end up with a page with no javascript at all. 我尝试删除所有其他Javascript文件,以确保不会被覆盖,而最终导致页面完全没有JavaScript。 I even tried recreating project and it still did not work. 我什至尝试重新创建项目,但仍然无法正常工作。 I tried moving my js file to the top in case it had something to do with load order. 我尝试将js文件移到顶部,以防它与加载顺序有关。 I tried a couple other things but in the course of three days of searching with little to no progress, I can't remember exactly what they were. 我尝试了其他几件事,但在三天的搜索过程中,几乎没有进展,甚至没有进展,我不记得它们到底是什么。 The only thing I haven't done is put the script itself directly in the page. 我唯一没有做的就是直接将脚本本身放在页面中。

I am using Windows 10, tried developing in VisualStudio 2015 and rolled back to 2013 due in part to not being able to alter my advanced properties to set the JS content to embedded (another suggestion) in 2015. The projects individual pieces were originally done in VS2013 on Windows8.1, so I am wondering if there is some sort of compatibility issue that I am overlooking. 我正在使用Windows 10,尝试在VisualStudio 2015中进行开发,然后回溯至2013年,部分原因是无法在2015年更改我的高级属性来将JS内容设置为嵌入(另一个建议)。项目的各个部分最初是在Windows8.1上的VS2013,所以我想知道是否存在某种我忽略的兼容性问题。

I am not sure what code to offer here but this is my current head to my html: 我不确定要在此处提供什么代码,但这是我目前对html的了解:

A Plea for Red Wolves 红狼的恳求

<!--Styles-->
<link rel="stylesheet" type="text/css" href="Site.css">
<link href='http://fonts.googleapis.com/css?family=Yesteryear' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<!--Scripts-->
<script src="~/Scripts/animalpagination.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

Here is my JS. 这是我的JS。 It takes a four button pagination styled element that targets information on the webpage and displays it when the button state is active. 它采用四个按钮分页样式的元素,该元素定位网页上的信息,并在按钮状态为活动状态时显示该信息。

//assign button a value, remove active, add active to clicked

$('.animalbutton').on('click', function () {
    var animaltoShow = $(this).attr('rel');
    $('#' + animaltoShow).button();
    $('.animalbutton').removeClass('active');
    $(this).addClass('active');

    //change active class on the content

    $('.animal').removeClass('active');
    $(this).show(function () {
        $("#" + animaltoShow).addClass('active', isactive);
    })

    function isactive() {
        //check if active, toggle visibility
        if (!'.animal .active') {
            document.getElementByClass('.animal').style.visibility = 'hidden';
        }
        else {
            document.getElementByClass('.animal .active').style.visibility = 'visible';
        }
    }

})

I am a newb developer so there are likely to be problems in my code. 我是一名新手开发人员,因此我的代码中可能存在问题。 Right now I am primarily focused on getting that file included in the browser, though suggestions are always welcome. 目前,我主要致力于将文件包含在浏览器中,尽管总是欢迎提出建议。

Have you tried including JQuery before your code ? 您是否尝试过在代码之前包含JQuery? Since you're using JQuery in it, you must have it included before ... 由于您在其中使用JQuery,因此必须先将其包括在内。

Also, you may try out to use a few : 另外,您可以尝试使用以下几种方法:

console.log('everything is fine here');

to check out if your code is working at each step. 检查您的代码是否在每个步骤中都有效。 Good luck with that ! 祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM