How do I set focus to a text box in Blazor
问题 How do I set focus to a textbox in Blazor? So far the only way we have found is with JavaScript. 回答1: 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