如何让 jquery 在 asp.net mvc 中工作

发布于 2024-11-26 10:33:06 字数 1147 浏览 0 评论 0原文

我已经读过这些书,我已经用谷歌搜索了它,但我仍然没有得到任何结果。我有一个 MVC 应用程序,我将对 jquery 的引用放入母版页中,并注册初始化代码,如下所示:

<script src="~/Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="~/Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />

<script>
    $(function () {
        $("#txt").click(function () {
            var $ctrl = $('<input/>').attr({ type: 'text', name: 'text', value: 'text' }).addClass("text");
        $("#holder").append($ctrl);
    });

});
</script>

该代码的目的是将文本框添加到 id 为“holder”的 div 中当按下 ID 为“txt”的按钮时。我从网上某处获得了这段代码,并且在该页面上运行良好。我再也找不到它了,否则我会添加链接。 我这样制作按钮:

<input type="button" id="txt" value="Add TextBox" />
<div id="holder"> this is the holder </div>

但是,按下按钮时绝对没有任何反应。我只想知道 JQUERY 代码去哪里?我是否将所有功能都放在母版页的头部?我要把它放在每个aspx页面中吗?我是否将其放在单独的文件中?如果是,我在哪里以及如何引用它?所有的书对我来说都没有任何意义,我只是从 jquery 开始,这个驼峰正是阻止我开始进入它的原因。 jquery代码去哪里了?请帮忙。

I have read the books, I have googled it to death, but I still get no results. I have an MVC application, and i put the reference to jquery into the master page, as well as the registering the initialization code like so:

<script src="~/Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="~/Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />

<script>
    $(function () {
        $("#txt").click(function () {
            var $ctrl = $('<input/>').attr({ type: 'text', name: 'text', value: 'text' }).addClass("text");
        $("#holder").append($ctrl);
    });

});
</script>

What that code is meant to do is to add a text box to a div with id "holder" when the button with id "txt" is pressed. I got this code from online somewhere, and it works fine from that page. I can't find it anymore or I would have included the link.
I made the button like this:

<input type="button" id="txt" value="Add TextBox" />
<div id="holder"> this is the holder </div>

However, when pressing the button absolutely nothing happens. All I wanna know is WHERE DOES THE JQUERY CODE GO? Do i put all the functions in the head of the master page? do i put it in every aspx page? do i put it in a separate file and if so where and how do i reference it? all the books don't make any sense to me, i'm just starting with jquery and this hump is what's stopping me from even starting to get into it. Where does the jquery code go? please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玉环 2024-12-03 10:33:06
<script src="@Url.Content("~/Scripts/jquery-1.4.1.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.4.1.js")" type="text/javascript"></script>
无所谓啦 2024-12-03 10:33:06

请参阅此处: http://jsfiddle.net/UukmR/1/

它工作正常吗?

确保所有脚本引用都是正确的,并将它们包含在母版页的标题中。

See here: http://jsfiddle.net/UukmR/1/

It works fine?

Make sure all your script references are correct, and include them in the Head of your master page.

雨的味道风的声音 2024-12-03 10:33:06

首先,您的 jQuery 代码无法运行的原因是 ~ 对浏览器没有意义。这需要切换为绝对引用:

<script src="/Scripts/jquery-1.4.1.js" type="text/javascript"></script>

..或者需要使用运行服务器端的代码将其转换为绝对引用:

// Using the ascx view engine
<script src="<%: Url.Content("/Scripts/jquery-1.4.1.js") %>" type="text/javascript"></script>
// Using the razor view engine
<script src="@Url.Content("/Scripts/jquery-1.4.1.js")" type="text/javascript"></script>

除此之外,您可以自由地将 jQuery 代码放在最有意义的地方。我个人发现的最佳策略是将尽可能多的 javascript 放入在头部引用的静态 javascript 文件中。除了将 javascript 和标记分开(看起来更好)之外,这还有一个额外的好处:将大量静态代码放入可以由浏览器缓存的静态文件中,从而减少跨越与每个请求连线。

PS:-vsdoc.js 文件纯粹是为了智能感知而存在,不应包含在实际标记中。当VS.NET知道你正在使用相应的jquery文件时,它会自动检测它的存在并使用它。

First of all, the reason your jQuery code isn't getting run is because ~ doesn't make sense to the browser. This needs to either be switched to an absolute reference:

<script src="/Scripts/jquery-1.4.1.js" type="text/javascript"></script>

.. or it needs to be converted into an absolute reference using code that runs server-side:

// Using the ascx view engine
<script src="<%: Url.Content("/Scripts/jquery-1.4.1.js") %>" type="text/javascript"></script>
// Using the razor view engine
<script src="@Url.Content("/Scripts/jquery-1.4.1.js")" type="text/javascript"></script>

Beyond that, you're free to put your jQuery code wherever it makes the most sense. The best strategy I've personally found is to put as much javascript as possible into a static javascript file that gets referenced in the head. In addition to keeping your javascript and markup separated (which just looks a lot nicer), this has the added benefit of putting a lot of static code into a static file which can be cached by the browser, thereby reducing the amount of bytes crossing the wire with each request.

PS: The -vsdoc.js file is purely there for the sake of intellisense, and should not be included in the actual markup. VS.NET will automatically detect its presence and use it when it knows that you're using the corresponding jquery file.

知足的幸福 2024-12-03 10:33:06

您的代码不应该

<script>
    $(function () {
        $("#txt").click(function () {
            var $ctrl = $('<input/>').attr({ type: 'text', name: 'text', value: 'text' }).addClass("text");
        $("#holder").append($ctrl);
    });

});
</script>

包含在 jquery $(document).ready() 调用中吗?

所以尝试这样的事情:

<script>
$(document).ready(function(){
    $("#txt").click(function () {
        var $ctrl = $('<input/>').attr({ type: 'text', name: 'text', value: 'text' }).addClass("text");
        $("#holder").append($ctrl);
    });
});
</script>

shouldn't your code:

<script>
    $(function () {
        $("#txt").click(function () {
            var $ctrl = $('<input/>').attr({ type: 'text', name: 'text', value: 'text' }).addClass("text");
        $("#holder").append($ctrl);
    });

});
</script>

be contained within the jquery $(document).ready() call?

so try something like this:

<script>
$(document).ready(function(){
    $("#txt").click(function () {
        var $ctrl = $('<input/>').attr({ type: 'text', name: 'text', value: 'text' }).addClass("text");
        $("#holder").append($ctrl);
    });
});
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文