javascript <head> 和 <asp:Content ID="HeaderContent" ...> 之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14984997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between <head> and <asp:Content ID="HeaderContent" ...>?
提问by Coffee
This may be a newbie question, but i'm pretty new to asp.net & C# etc.
这可能是一个新手问题,但我对 asp.net 和 C# 等很陌生。
I'm working with an ASP.net website, and I'm curious about the structure of it (after automatically creating a web project), specifically the following:
我正在使用一个 ASP.net 网站,我很好奇它的结构(在自动创建一个 web 项目之后),特别是以下内容:
I see that in Default.aspx , I have a tag like this:
我在 Default.aspx 中看到,我有一个这样的标签:
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>**strong text**
But in Site.master, I have this:
但是在 Site.master 中,我有这个:
<head runat="server">
*etc*
</head>
So where would I put code if I wanted to include JavaScript code to run, on page load?
那么,如果我想在页面加载时包含要运行的 JavaScript 代码,我应该把代码放在哪里?
回答by codingbiz
I believe you can put your code in any of them. The first one is for adding code or script used by all content pages(that using this master page file) while the second one is if you want to to add script or code from content pages(that should be used only for this specific page)
我相信您可以将您的代码放入其中任何一个中。第一个用于添加所有内容页面(使用此母版页文件)使用的代码或脚本,而第二个用于添加内容页面中的脚本或代码(应仅用于此特定页面)
//in the Master page, the content here is used by all content pages
<head runat="server">
*etc*
</head>
and
和
//this is specific to the content page that use it. This section needs to be supplied in content pages
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
That section needs to be supplied in each content page and it will be exclusive to that page - no other page can use the script in that section
该部分需要在每个内容页面中提供,并且该部分将独占该页面 - 没有其他页面可以使用该部分中的脚本
回答by KevinD
asp:Content ID="HeaderContent"is a content region. Anything within that tag will get embedded in the associated ContentPlaceHolder in the master page when it is generated.
asp:Content ID="HeaderContent"是一个内容区域。生成时,该标记中的任何内容都将嵌入到母版页中关联的 ContentPlaceHolder 中。
headis a standard html markup, indicating the page head elements. Typically, the HeadContent placeholder is inside the headtag on the master page.
head是标准的 html 标记,表示页面的头部元素。通常,HeadContent 占位符位于母版页上的head标记内。
回答by user1976537
The head element, container for all the head elements, must use a title for the document. Some other elements it can include: style, base, link, meta, script, noscript.
head 元素,所有 head 元素的容器,必须使用文档的标题。它可以包括的其他一些元素:样式、基础、链接、元、脚本、noscript。
The asp: Content ID = "HeaderContent" is a content element of the master page. Have a look at the Plugging in Content part of the following link for detailed information on this: http://odetocode.com/articles/419.aspx
asp:Content ID = "HeaderContent" 是母版页的内容元素。有关详细信息,请查看以下链接的插入内容部分:http: //odetocode.com/articles/419.aspx
回答by ?brahim Y?lmaz
I think you asked when you want to use JavaScript where you put JS in your code.You can put anywhere you wish in asp side between script block such as:
我想你问过你什么时候想使用 JavaScript 把 JS 放在你的代码中。你可以把你想要的任何地方放在脚本块之间的 asp 端,例如:
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript">
function Onclick(){
//some codes
}
</script>
</asp:Content>
or
或者
<head runat="server">
<script type="text/javascript">
function Onclick(){
//some codes
}
</script>
</head>
Also you can put JS outside this tag. You only should use tag.
你也可以把 JS 放在这个标签之外。你只应该使用标签。