我在没有母版页的 aspx 页面中使用 JQuery 时没有任何问题,但是当我尝试在有母版页的页面中使用它时,它不起作用,所以我最终将 jquery 文件和其他脚本文件放在页面而不是母版。 现在,如果我有 10 页,我会对所有 10 页执行此操作,但我知道这是不正确的。 在下面的示例母版页中,我将把脚本文件放在哪里。
<html>
<head runat="server">
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<asp:ContentPlaceHolder ID="ContentPanel" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
我最近使用了 fancybox 插件,我所做的不是将 jquery 脚本和 fancybox 脚本放在母版页中,因为我对其工作感到沮丧,我只是将其放在我希望脚本运行的页面中,特别是在底部,就在关闭 asp:Content 之前。 当然,现在我有一个问题,如果我想在其他页面中使用 fancybox 插件,我会将 jquery 脚本和 fancybox 脚本放在所有 5 个页面上,而不仅仅是母版页上。 处理母版页时,使用上面的示例,所有内容都去哪里了?
I have no issues when doing using JQuery in a aspx page without a masterpage, but when I try to use it in pages that have a masterpage, it doesn't work, so I end up putting the jquery files and other script files in the page instead of the master. Now if I have 10 pages, I am doing this for all 10, which I know is incorrect. In the sample masterpage below, where would I put my script files.
<html>
<head runat="server">
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<asp:ContentPlaceHolder ID="ContentPanel" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
I recently used the fancybox plugin and what I did was instead of putting the jquery script and fancybox scripts in the masterpage because I got frustrated on getting it to work, I just put it in the page where I wanted the script to run, specifically at the bottom, right before the closing asp:Content. Of course, now I have the issue of, if I wanted to use the fancybox plugin in other pages, I would put the jquery script and fancybox script on all 5 pages instead of just the masterpage. When dealing with masterpages, where does everything go using my example above?
发布评论
评论(4)
您可以像平常一样在母版页中声明主要 jQuery 脚本:
然后可以在引用 Head ContentPlaceholder 的内容控件中加载任何特定于页面的 JS 文件。
但是,更好的选择是查看 ScriptManager< /a> 和 ScriptManagerProxy 控件 - 这些可以为您提供对向客户端提供 JS 文件的方式的更多控制。
因此,您可以在母版页中放置一个 ScriptManager 控件,并在其中添加对 jQuery 核心代码的引用:
然后,在需要一些自定义 JS 文件或 jQuery 插件的页面中,您可以:
ScriptManager 允许您执行一些操作,例如控制页面上脚本的呈现位置 LoadScriptsBeforeUI(或者更好,将其设置为 False 之后)。
You would declare your main jQuery scripts within the master page, as you would normally:
And then any page specific JS files could be loaded within the Content controls that reference the Head ContentPlaceholder.
However, a better option would be to look into the ScriptManager and ScriptManagerProxy controls - these can provide you with a lot more control over the way your JS files are served to the client.
So you would place a ScriptManager control in you master page, and add a reference to the jQuery core code in that:
Then, in your page that requires some custom JS files, or a jQuery plugin, you can have:
The ScriptManager allows you to do things like control where on the page scripts are rendered with LoadScriptsBeforeUI (or better yet, after by setting it to False).
我用这个方法。
只需将其放在您的标签上方即可...
I use this method.
Just place it above your tag...
好吧,为您的脚本使用不同的内容占位符。 它应该如下所示
将此标记放置在母版页的底部,靠近
标记。 这将允许您在母版页以及您的页面上添加脚本。 通过查看页面源代码并确保以正确的方式呈现 HTML,确保脚本以正确的顺序加载。 祝你好运。
哦,还有一件事,请确保 jQuery 和 FancyBox 在您可能拥有的任何其他 js 之前加载,否则它将无法工作。 Javascript 按照调用的顺序加载,jQuery 必须首先加载!
Alright use a different contentplaceholder for your script. It should look like this
Place this tag at the bottom of your masterpage, close to the
</body>
tag. This will allow you add scripts on the masterpage and also on your pages as well. Make sure that your scripts are loading in the right order by viewing the page source and ensuring the HTML rendered in the right way. Good luck.Oh one more thing, make sure jQuery and then FancyBox load up before any other js you may have out there or else it won't work. Javascript loads in the order it was called, jQuery must load first!
这是母版页中的工作方式:
对于脚本文件:
对于 CSS 文件:
附加说明:
(FileName.min.css) 以减少加载时间并提高 SEO。
之前,将脚本放在
之前
提高性能并改善 SEO。
runat="server"
。 该脚本不得包含runat="server"
,因为它已使用服务器运算符<%= %>
。This is what will work inside a Master page:
For Script file:
For CSS file:
Additional Notes:
(FileName.min.css) to reduce loading time and improve SEO.
</head>
and the script before the</body>
toenhance performance and improve SEO.
runat="server"
for the stylesheet only. The script must not haverunat="server"
because it already uses server operators<%= %>
.