无法将焦点设置在表格行上

问题描述 投票:1回答:1

我试图在组件初始化时将焦点设置在表行上,并在使用interop时得到null

<table  class='table'>
     <thead>
         <tr>value</tr>
    </thead>
     <tbody>          
        @foreach (var elem in this.data) {
           <tr id="@elem.toString()"><td>@elem</td></tr>
     </tbody>
 </table>


@functions()
{
     protected int []data=new int[]{1,2,3,34};
     protected override async Task OnInitAsync() {
                if (data.Length > 0) {
                    var elementName= data.First().ToString();
                    await JSRuntime.Current.InvokeAsync<string>("methods.focus", elementName);;
                }
            }

}

互操作

window.methods={
    focus: function (elementName) { //i get the right id 
            var element = document.getElementById(elementName);//returns null
            element.focus();
        }
}

P.S我在浏览器中调试时在elementName方法中得到正确的focus,但document.getElementByIdreturns是一个null元素。我需要先获取表元素然后搜索其中的行吗?

javascript interop blazor
1个回答
1
投票

您无法将焦点设置为td或tr元素。但是,您可以在td元素中放置一个输入元素,然后设置焦点。

希望这可以帮助...

© www.soinside.com 2019 - 2024. All rights reserved.