jQuery with .Net
So I have a very simple .Net page that uses JQuery. When I place the code directly on the page the script executes fine, but when I call from an external js page it fails.
$(document).ready(function () {
$("#MainContent_Button1").click(function () {
alert("Hello world!");
});
});
(jQuery)
<asp:Button ID="Button1" CssClass="Button1" runat="server" Text="CLICK ME FOO" />
When I link like so:
<script type="text/javascript" src="menu.js"></script>.
It doesn't work, but if I place on the page it w开发者_StackOverfloworks fine.
It's supposed to be src="" in the script tag.
<script type="text/javascript" src="menu.js"></script>
Most likely the javascript file you are referencing externally is not loading. Use chrome to make sure you are getting a 200 message for that file and not a 404. Also, make sure in your external javascript file you are including the jquery document load method exactly like you have above. You have to ensure the button has been placed in the DOM before you can bind any events to it. Also I would recommending using ClientIDMode="static" so you can reference the button with the actual id of button in the jQuery selector.
精彩评论