【问题标题】:Asp.Net Mvc - Html.TextBox - Set Autofocus propertyAsp.Net Mvc - Html.TextBox - 设置自动对焦属性
【发布时间】:2011-03-01 04:57:48
【问题描述】:

在 Html 5 中,文本框上有一个新属性,称为 autofocus。

问题是它是一个布尔值(有或没有)

它应该看起来像:

<input name="a" value="" autofocus>

我试过了:

<%= Html.TextBox( "a", null, new { autofocus } ) %>

但是,它给了我一个错误,因为我没有设置自动对焦的值...

我知道我可以手动完成,但我可以用 Html.TextBox 完成吗?

【问题讨论】:

    标签: asp.net-mvc .net-3.5 html-helper


    【解决方案1】:

    试试&lt;%= Html.TextBox( "a", null, new { autofocus = "" } ) %&gt;

    根据HTML5 spec on boolean attributes

    如果该属性存在,它的值必须是空字符串或与属性的规范名称匹配的不区分大小写的ASCII值,没有前导或尾随空格。

    所以

    • &lt;input name="a" value="" autofocus&gt;
    • &lt;input name="a" value="" autofocus=""&gt;
    • &lt;input name="a" value="" autofocus="autofocus"&gt;

    应该是有效的。

    【讨论】:

      【解决方案2】:

      在 XHTML 中,启用这种布尔属性的标准方法是:

      <input name="a" value="" autofocus="autofocus" />
      

      因此,假设它在 HTML5 中仍然有效,您可以使用以下代码:

      <%=Html.TextBox( "a", null, new { autofocus: "autofocus" } ) %>
      

      【讨论】:

        【解决方案3】:

        此外,您还可以执行以下操作以及其他一些属性:

        @Html.TextBoxFor(m => m.Email, new { @class = "class1", @placeholder = "Email", @autofocus = "autofocus" })
        

        注意:自动对焦的唯一问题是,在 IE 浏览器中,当输入控件处于焦点时,占位符文本不会显示(这是 IE 的问题)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-15
          • 2013-05-23
          • 2012-12-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多