简体   繁体   English

C# - Blazor @onlick 如何获得 HTML 元素点击

[英]C# - Blazor @onlick how to get HTML element clicked on

I am currently learning C# Blazor framework and I am faced with question, how to get HTML element I clicked on?我目前正在学习 C# Blazor 框架,我面临一个问题,如何获得我点击的 HTML 元素?

I need it to get it's position in DOM and in parent HTML element.我需要它来获取它在 DOM 和父 HTML 元素中的 position 。

For example, classic way with JQuery:例如,使用 JQuery 的经典方式:

$('selector').click(function (event) {
    alert($(this).index());
});

http://jsfiddle.net/bCTAh/ http://jsfiddle.net/bCTAh/

I know, that there is @onclick attribute in Blazor, for example:我知道,Blazor 中有@onclick 属性,例如:

<tr @onclick="OnYes">...</tr>

@functions {
    ElementReference inputTypeFileElement;

    public async Task MainTableOnClick(MouseEventArgs e)
    {
        Console.WriteLine("clicked!");
    }
}

How can I get index of TR HTML element that was clicked on?如何获取被点击的 TR HTML 元素的索引?

My task is convert Windows Form app to Web-version.我的任务是将 Windows 表单应用程序转换为 Web 版本。 The old Windows Form has DataGridView, each row of that has onClick event and Tag Object too.旧的 Windows 表单有 DataGridView,每一行都有 onClick 事件和标签 Object。 When some of row of the DataGridView clicked, onClick get's Tag Object of the row and used it to fill data to another DataGridView's on the form.当点击 DataGridView 的某些行时,onClick 获取该行的 Tag Object 并用它来将数据填充到表单上的另一个 DataGridView 中。 So, I need to know, what row clicked to get data from some object (it can be DataTable, or, to be more simply, Array).所以,我需要知道,点击哪一行从一些 object 中获取数据(它可以是 DataTable,或者更简单地说,是 Array)。 Based on index of row and index in Array, I need to get data for filling another tables on Web Page.基于数组中的行索引和索引,我需要获取数据以填充 Web 页面上的其他表。

So, first table is Clients (name, surname, etc...).因此,第一个表是客户(姓名、姓氏等)。

When some of row with Client has been clicked, I need to get row (Client) index in the table.当单击带有客户端的某些行时,我需要获取表中的行(客户端)索引。 By that index I'll get data from Array of Clients.通过该索引,我将从客户端数组中获取数据。 By found Client object I plan to fill another tables on the page dynamically.通过找到客户端 object 我计划动态填充页面上的另一个表。

In this case, you can use JSInterop在这种情况下,您可以使用JSInterop

Or或者

<tr @onclick="() => TrClickedAtIndex(0)">...</tr>

@code {
    ElementReference inputTypeFileElement;

    public void TrClickedAtIndex(int index)
    {
        Console.WriteLine($"tr clicked at index {index}!");
    }
}

If you want to get the element you can use a @ref to return an element, but generally in Blazor you're not interest in the elements (as you might in JQuery) but what they relate to.如果要获取元素,可以使用@ref返回元素,但通常在 Blazor 中,您对元素不感兴趣(就像在 JQuery 中一样),而是它们与什么相关。

I created this simple BlazorFiddle that lists three strings in a table.我创建了这个简单的 BlazorFiddle,它在一个表中列出了三个字符串。 It's similar to the other answer but it demonstrates a little wrinkle - when passing a value from a loop or enumeration you need to assign it.它类似于其他答案,但它表现出一点皱纹 - 从循环或枚举传递值时,您需要分配它。

https://blazorfiddle.com/s/br8lwsl2 https://blazorfiddle.com/s/br8lwsl2

This shows a table for three strings.这显示了一个包含三个字符串的表。 When you click a row, it passes the data item from the clicked row - not just an index.当您单击一行时,它会从单击的行传递数据项 - 而不仅仅是索引。

    int index = i;
    <tr @onclick="()=> ClickedRow(list[index])" ><td>

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM