且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Blazor中将焦点设置为文本框

更新时间:2023-01-12 18:33:56

没有其他方法可以执行此操作...您可以使用JSInterop来执行此操作,如下所示:

There is no other way to do it... You can use JSInterop to do this, as follows:

 <input type="text" @ref="myref"/>

 @code {

    private ElementReference myref;
    [Inject] IJSRuntime JSRuntime { get; set; }

     protected override async Task OnAfterRenderAsync(bool firstRender)
    {
         if (firstRender)
        {
            await 
        JSRuntime.InvokeVoidAsync("exampleJsFunctions.focusElement", myref);
        }
   }
 }

JavaScript

<script>

    window.exampleJsFunctions =
    {
        focusElement: function (element) {
           element.focus();
        }
    };
</script>

希望这对您有帮助...

Hope this helps...