简体   繁体   English

标签助手 - 自关闭 HTML 标签是一个坏习惯?

[英]Tag Helpers - Self closing HTML tags is a bad habit?

I am incorporating TagHelpers in my MVC vNext project, and I realized they don't work when I self close the HTML tags.我在我的 MVC vNext 项目中加入了TagHelpers ,当我自己关闭 HTML 标签时,我意识到它们不起作用。

@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"

<label asp-for="FirstName"/> <!-- self closing tag -->

<span asp-validation-for="FirstName"/> <!-- self closing tag -->

And, insteand when I put the closing tags, I see the values being displayed.而且,当我放置结束标签时,我会看到正在显示的值。

<label asp-for="FirstName"></label>

<span asp-validation-for="FirstName"></span>

Generated HTML生成的 HTML

<label for="FirstName">FirstName</label>
<span class="field-validation-error" data-valmsg-for="FirstName" data-valmsg-replace="true">
    <span id="FirstName-error" class="">The FirstName field is required</span>
</span>

What I want to know, what difference does it make?我想知道什么,它有什么区别? And, is self closing the paired tags a bad habit.而且,自我关闭配对标签是一个坏习惯。 If you know some article which talks about this design principle, please share with me, it would be much appreciated.如果您知道一些讨论此设计原则的文章,请与我分享,将不胜感激。

The reason they do not work is because the "MVC tag helpers do not change whether an element is self-closing or not" (from comments in issue #4475 ).它们不起作用的原因是因为“MVC 标记助手不会更改元素是否自关闭”(来自问题#4475 中的评论)。

It is a known issue in ASP.NET and the plan is to show a warning in VS IDE under the scenario of a non-void element having an end tag (see issue #398 ).这是 ASP.NET 中的一个已知问题,计划是在具有结束标记的非空元素的情况下在 VS IDE 中显示警告(请参阅问题#398 )。 One of ASP.NET developers commented in issue #1302 that:一位 ASP.NET 开发人员在问题#1302中评论说:

"this is the current design but we have a few issues (open and closed) on the behaviour" “这是当前的设计,但我们在行为上有一些问题(开放和封闭)”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM