2014-07-24 3 views
0

Comment utiliser jQuery dans un fichier javascript externe dans ASP.NET?jQuery dans un fichier javascript externe dans ASP.NET

Ceci est mon code:

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script src="~/Javascript/Javascript.js" runat="server"></script> 
    <script src="~/Javascript/jquery-2.1.1.js"></script> 
</head> 

Mais dans le fichier Javascript.js, je ne peux pas utiliser jQuery sélecteur $(). Lorsque je l'utilise ne fonctionne pas.

javascript.js:

$(document).ready(function() { 
    alert('Hello'); 
}); 

Ce code ne fonctionne pas. Aussi IntelliSense ne fonctionne pas dans Javascript.js.

Répondre

2

Vous devez inclure jQuery avant javascript.js

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script src="~/Javascript/jquery-2.1.1.js"></script> 
    <script src="~/Javascript/Javascript.js" runat="server"></script> 
</head> 

Vous avez dit que vous avez ces erreurs dans votre console développeur:

Failed to load resource: the server responded with a status of 404 (Not Found) localhost:46316/~/Javascript/Javascript.js 
Failed to load resource: the server responded with a status of 404 (Not Found) localhost:46316/~/Javascript/jquery-2.1.1.js 
Uncaught TypeError: undefined is not a function 

Vous pouvez régler ces en définissant les chemins d'accès à vos fichiers correctement :

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script src="/Javascript/jquery-2.1.1.js"></script> 
    <script src="/Javascript/Javascript.js" runat="server"></script> 
</head> 

Rendre IntelliSense travail en mettant cette ligne de code à Javascrip t.js (comme vous vous réalisé):

/// <reference path="jquery-2.1.1.js" /> 

Lire la documentation des fonctionnalités IntelliSense et directives de référence ici: http://msdn.microsoft.com/en-us/library/bb385682.aspx#Features

+0

J'ai essayé, mais ne fonctionne pas – Natiq

+0

Pouvez-vous coller dans le code de Javascript.js? –

+0

Par exemple, ce code ne fonctionne pas. Aussi intellisense ne fonctionne pas $ (document) .ready (function() { alert ('Hello'); }); – Natiq

Questions connexes