【问题标题】:Blazor JS Interop getElementById to get component referenceBlazor JS 互操作 getElementById 以获取组件引用
【发布时间】:2021-10-03 16:51:44
【问题描述】:

我需要在 Blazor 中通过其 ID 获取对组件的引用。 注意:不使用 ElementReference,因为它用于通用代码。

我使用这个 JS 互操作脚本来做焦点,它对我来说很好:

  public async Task Focus(string elementId){
              await js.InvokeVoidAsync("eval", $@"document.getElementById(""elementId}"").focus()");
    }

但是我需要从 Blazor 获取的只是对元素(组件)的引用,即返回: document.getElementById(""{elementId}"") 将 elementId 作为参数发送。

有可能吗?

编辑:我已尝试使用此脚本,但从 Blazor 调用它时总是返回 null:

<script>
        window.GetElementById = (myId) => {
            return document.getElementById(myId);
        }
</script>

【问题讨论】:

  • 你能扩展用例吗?你可能不需要这个。
  • 你想要 javascript HTMLElement 还是 blazor ElementReference?
  • 酷开发者,这对我来说已经足够了,在 Blazor 中返回这个 JS 表达式:document.getElementById ("myId")

标签: blazor getelementbyid blazor-jsinterop


【解决方案1】:

更新

专门回答你的问题。

您正在使用 InvokeVoidAsync 调用,如您所见,它返回一个 void。如果您使用 InvokeAsync 调用,则会返回一个空的 JsonDocument 对象。

所以不,您无法从 js 调用中获取可用的引用。

澄清问题之前的上一个答案

================================================ ============== 如果我正确阅读了您的问题,您希望将焦点设置为“一般”生成的 html 元素 id

下面的答案显示了如何使用Elementid 设置焦点。

使用以下代码将site.js 添加到wwwroot

window.SetElementFocus = function (element) {
    element.focus();
    return element === true;
}

window.SetFocusByElementId = function (elementId) {
    var element = document.getElementById(elementId);
    element.focus();
    return element === true;
}

在你的启动文件中引用 js 文件(这是用于服务器的)

    <script src="/site.js"></script>
    <script src="_framework/blazor.server.js"></script>

然后这里是一个演示页面。

@page "/Test6"
<div class="m-2">
    <input type="text" @ref="Input1" placeholder="Input 1" id="input1" />
</div>

<div class="m-2">
    <input type="text" @ref="Input2" placeholder="Input 2" id="input2" />
</div>

<div class="m-2">
    <input type="text" @ref="Input3" placeholder="Input 3" id="input3" />
</div>

<div class="m-2 p-2">
    <button class="btn btn-dark me-1" @onclick="() => Focus(1)">Element Focus 1</button>
    <button class="btn btn-dark me-1" @onclick="() => Focus(2)">Element Focus 2</button>
    <button class="btn btn-dark me-1" @onclick="() => Focus(3)">Element Focus 3</button>
</div>

<div class="m-2 p-2">
    <button class="btn btn-secondary me-1" @onclick="() => FocusById(1)">Id Focus 1</button>
    <button class="btn btn-secondary me-1" @onclick="() => FocusById(2)">Id Focus 2</button>
    <button class="btn btn-secondary me-1" @onclick="() => FocusById(3)">Id Focus 3</button>
</div>


@code {

    [Inject] private IJSRuntime _js { get; set; }

    private ElementReference Input1;
    private ElementReference Input2;
    private ElementReference Input3;

    private async Task Focus(int no)
    {
        var input = no switch
        {
            1 => Input1,
            2 => Input2,
            _ => Input3
        };
        await _js.InvokeAsync<bool>("SetElementFocus", input);
    }

    private async Task FocusById(int no)
    {
        var input = no switch
        {
            1 => "input1",
            2 => "input2",
            _ => "input3"
        };
        await _js.InvokeAsync<bool>("SetFocusByElementId", input);
    }
}

【讨论】:

  • 不是很专注,因为它对我来说已经很有效了。我想要的只是在 Blazor 中获取元素,即。 e.这个JS表达式的返回:document.getElementById("myId"),不关注元素;从 Blazor 发送 myId 作为参数。
  • 您希望返回什么类型的“对象”,您想用它做什么?你用InvokeVoidAsync 调用它,它返回一个void。使用 InvokeAsync 调用它会得到一个不包含任何内容的 JsonDocument 对象。
  • 只是期望返回一个对象 .net 类型。 InvokeVoidAsync 只是作为一个工作重点示例,但我需要的是返回元素,使用我在最后添加的脚本,但它返回 null。
  • 无论您打算如何处理返回的对象,您都需要重新考虑您的设计。你要它干什么?
  • 我真的对 JavaScript 知之甚少,所以我问。如果没有其他选择,那么我会寻找其他解决方案。我非常感谢您的帮助,但我的问题是具体是否可行。根据您的回答,我必须假设没有。
猜你喜欢
  • 1970-01-01
  • 2020-07-15
  • 2021-02-24
  • 2023-03-25
  • 2015-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-15
相关资源
最近更新 更多