【问题标题】:Autofocus on the textbox in html razor code自动聚焦在 html 剃刀代码中的文本框上
【发布时间】:2014-09-04 09:51:26
【问题描述】:

我希望在加载页面时将光标聚焦在文本框上。我尝试通过添加 autofocus="" 但我找不到任何区别。

任何帮助将不胜感激!

@Html.TextBoxFor(
    m => m.UserName, 
    "@xyz.com", 
    new { @class = "form-control input-lg", @id = "username"})

【问题讨论】:

  • @Html.TextBoxFor(m => m.UserName, "@xyz.com", new { @class = "form-control input-lg", @id = "username", autofocus = "autofocus" }),但autofocus = "" 也应该可以工作
  • 对我来说很好。您在这里显示的第二个参数是什么?它看起来不像是格式字符串!
  • 您知道autofocus 属性在IE9 及更早版本中不受支持吗?

标签: asp.net-mvc-3 razor razor-2


【解决方案1】:

如果你对 jquery 没问题,你可以尝试使用

<script type="text/javascript">
    $(function () {
        $("#username").focus();
    });
</script>

【讨论】:

    【解决方案2】:

    你可以像这样使用纯 java 脚本:

    <script type="text/javascript">
        document.getElementById("username").focus();
    </script>
    

    【讨论】:

      【解决方案3】:

      autofocus 属性并非在所有浏览器中都实现。它是浏览器的一个很好的子集,大多数较新的浏览器都有它,但不是全部。很可能您使用的浏览器不支持autofocus 属性。 Dive into HTML 5 has a good listing of the browser support.

      【讨论】:

        【解决方案4】:
        <script type="text/javascript">
            $(document).ready(function(){
               $("#username").focus();
               });       
            }); </script>
        

        【讨论】:

          猜你喜欢
          • 2017-04-16
          • 1970-01-01
          • 2018-03-01
          • 2011-05-10
          • 2015-07-22
          • 1970-01-01
          • 1970-01-01
          • 2021-09-10
          相关资源
          最近更新 更多