为什么我得到错误消息“Element'style'不能嵌套在element'style'”? [英] Why do I get the error message "Element 'style' cannot be nested within element 'style'"?

查看:353
本文介绍了为什么我得到错误消息“Element'style'不能嵌套在element'style'”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要重写我的Razor页面中的一些< style> 元素,所以我已经在开头代码块后插入样式:

I need to override some <style> elements within my Razor page so I've inserted the styles immediately after the opening code block:

@{
    ViewBag.Title = "New Account";
    Layout = "~/Views/Shared/_Layout_Form.cshtml";
}
<style type="text/css">
    #main
    {
        height: 400px;
    }
</style>

页面在浏览器中正确显示,但Visual Studio绿色在 style> 抱怨:

The page renders correctly in the browser but Visual Studio green squiggles at <style> complaining that:

<Validation (XHTML 1.0 Transitional): Element 'style' cannot be nested within element 'style'>

我已经双重检查母版页 - 没有突出显示的问题。

I've doublechecked the master page - no problems highlighted there.

Visual Studio期待什么?在逐页重写样式的正确方法是什么?通过jQuery?

What's Visual Studio expecting at this point? What's the correct way to override styles on a page by page basis? Via jQuery?

推荐答案

样式元素不能嵌套在< body> < body> 的子元素,而应该转到< head> 元素

The style element cannot be nested under the <body> or child elements of the <body>, instead it should go to the <head> element of the page.

如果您尝试覆盖这样的样式,它们会通过 @RenderBody()插入到布局页面的默认部分。 ),其中我假定是在布局页面的< body> 内,而默认样式仍然保留在< head>

If you try to override the styles like this, they get inserted into the default section of your layout page by @RenderBody(), which I assume is inside the <body> of the layout page, while the default styles still stay in the <head>.

从内容页面覆盖布局页面的某些部分的正确方法是沿着这些行,使用Razor部分:

The proper way to override some part of the layout page from the content page would be something along these lines, using Razor sections:

<head>
@if (IsSectionDefined("Style"))
{
    @RenderSection("Style");
}
else
{
    <style type="text/css">
    /* Default styles */
    </style>
}
</head>
<body>
@RenderBody()
...



page.cshtml



page.cshtml

@{        
    Layout = "layout.cshtml";
}
@section Style
{
    <style type="text/css">        
    #main        
    {        
        height: 400px;        
    }
    </style>
}
<!-- Body content here -->
...

现在如果 Style section在内容页面上定义,其内容将覆盖布局页面的默认值。

Now if the Style section is defined on the content page, its contents will override the defaults from the layout page.

我建议您阅读有关Razor布局和部分的更多信息。有关ScottGu的一篇很好的文章可以找到此处

I suggest you read more on Razor layouts and sections. A nice article on this by ScottGu can be found here.

Visual Studio当构成单个页面的标记被分割在不同的文件之间时,有这样的问题。在大多数情况下,Visual Studio(或任何这样的IDE)没有办法知道如何将不同的页面片段动态放在一起。所以通常你不能避免一个项目上的一些警告。

Visual Studio has a problem when markup that makes up a single page is being split up between different files like this. In most cases there is no way for Visual Studio (or any such IDE for that matter) to know how the different page fragments will be dynamically put together in the end. So usually you can't avoid some of these warnings on a project.

我个人认为,如果我知道我在做什么,并且结果页通过标记验证( http://validator.w3.org/ )。

Personally I would ignore the warnings if I know what I'm doing and the resulting pages pass the markup validation (http://validator.w3.org/).

如果您真的想隐藏警告,则需要在Visual Studio中禁用相应的验证选项。例如。对于Visual Studio 2012中的HTML,可以在工具>选项>文本编辑器> HTML>验证。

If you really want to hide the warnings, you need to disable the appropriate validation options in Visual Studio. E.g. for HTML in Visual Studio 2012 this can be done in Tools > Options > Text Editor > HTML > Validation.

这篇关于为什么我得到错误消息“Element'style'不能嵌套在element'style'”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆