标签助手 - 自闭HTML标签是一个坏习惯? [英] Tag Helpers - Self closing HTML tags is a bad habit?

查看:166
本文介绍了标签助手 - 自闭HTML标签是一个坏习惯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的MVC vNext项目中加入了 TagHelpers ,并且当我自己关闭HTML标记时,我意识到它们不起作用。

  @addTagHelper*,Microsoft.AspNet.Mvc.TagHelpers

< label asp-for =FirstName/> <! - 自动关闭标签 - >

< span asp-validation-for =FirstName/> <! - 自动关闭标签 - >

而且,当我放置结束标记时,我看到显示的值。

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

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

生成的HTML

 < label for =FirstName>名字< / label> 
< span id =FirstName-errorclass => FirstName字段是必需的< / span>
< / span>

我想知道,它有什么不同?而且,自动关闭配对标签是一种坏习惯。如果你知道一些关于这个设计原则的文章,请与我分享,这将是非常感谢。

解决方案

原因他们不工作是因为MVC标签助手不会改变元素是否自闭合(来自

这是ASP.NET中的一个已知问题,计划是在VS IDE中显示警告非空元素具有结束标记的场景(请参阅问题#398 )。其中一位ASP.NET开发人员在#1302 的评论中指出:


这是目前的设计,但我们对行为有一些问题(打开和关闭)



I am incorporating TagHelpers in my MVC vNext project, and I realized they don't work when I self close the HTML tags.

@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

<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).

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). One of ASP.NET developers commented in issue #1302 that:

"this is the current design but we have a few issues (open and closed) on the behaviour"

这篇关于标签助手 - 自闭HTML标签是一个坏习惯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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