在 Html 5 中,文本框上有一个名为 autofocus 的新属性。
问题在于它是一个 bool 值(存在或不存在)
它应该看起来像:
<input name="a" value="" autofocus>
我尝试过:
<%= Html.TextBox( "a", null, new { autofocus } ) %>
但是,它给了我一个错误,因为我没有设置自动对焦的值......
我知道我可以手动完成,但是我可以使用 Html.TextBox 完成吗?
最佳答案
尝试<%= Html.TextBox( "a", null, new { autofocus = "" } ) %>
根据HTML5 spec on boolean attributes :
If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.
所以要么
-
<input name="a" value="" autofocus>
或 -
<input name="a" value="" autofocus="">
或 -
<input name="a" value="" autofocus="autofocus">
应该是有效的。
关于asp.net-mvc - Asp.Net Mvc - Html.TextBox - 设置自动对焦属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2998247/