我试图在组件初始化时将焦点设置在表行上,并在使用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.getElementById
returns是一个null元素。我需要先获取表元素然后搜索其中的行吗?
您无法将焦点设置为td或tr元素。但是,您可以在td元素中放置一个输入元素,然后设置焦点。
希望这可以帮助...