通过针对html元素的设置样式的html元素。

发布于 2025-02-07 02:17:35 字数 814 浏览 1 评论 0原文

说我有一个元素。我想根据《大火》中输入字段的值设置元素样式。我知道您可以通过使用HTML代码中的其他块来实现此操作,但是我想避免使用它,因为它将代码库非常快地生长。一个函数会更好,可以评估传入对象并进行样式。

例如,如果我的元素以下元素传递一个值,则值将传递给函数。

< th>< inputText占位符=“名字” id = firstName @bind-value =“ @filtermendel.firstname”> first</inputText&ltputText&

gt

    private async void UpdateVisibleData(object sender, FieldChangedEventArgs e){
        Console.WriteLine(e.FieldIdentifier.FieldName + " Change Detected");
        var fieldChanged = e.FieldIdentifier.FieldName;
        var IsEmptyString = FindEmptyStringInField(filterModel, fieldChanged);
        if(IsEmptyString){
            element.style.setProperty('--my-variable-name', 'hotpink');
           }
        }

;能够使style.setproperty零件工作。不确定还有哪些其他方法。向JSinterop开放。虽然我可以针对样式。

Say I have an element in Blazor. I want to set the elements style based on the value of the input field in Blazor. I am aware that you can do this by using if else blocks in the html code however I want to avoid this as it grows the code base extremely quick. A function would be better which evaluates the incoming object and styles it.

for instance if I have the element below which passes a value the value is passed to a function.

<th><InputText placeholder="First Name" id=FirstName @bind-Value="@filterModel.FirstName">First Name</InputText></th>

Function

    private async void UpdateVisibleData(object sender, FieldChangedEventArgs e){
        Console.WriteLine(e.FieldIdentifier.FieldName + " Change Detected");
        var fieldChanged = e.FieldIdentifier.FieldName;
        var IsEmptyString = FindEmptyStringInField(filterModel, fieldChanged);
        if(IsEmptyString){
            element.style.setProperty('--my-variable-name', 'hotpink');
           }
        }

I havent been able to get the style.setproperty part to work. not sure what other methods there are. Open to JSInterop. preferrable though if I can target the style.setproperty function and get this working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神也荒唐 2025-02-14 02:17:35

修订后的答案

,或者您可以这样使用jsinterop:

/wwwroot/js/example.js

export function examplefunction (Id, Color) {
    document.getElementById(Id).style.backgroundColor = Color;
}

剃须刀页面:

@inject IJSRuntime JS

@code {
    private IJSObjectReference? module;
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            module = await JS.InvokeAsync<IJSObjectReference>("import", "/js/example.js");
        }
    }
    private async void UpdateVisibleData(object sender, FieldChangedEventArgs e){
        var fieldChanged = e.FieldIdentifier.FieldName;
        await module.InvokeVoidAsync("examplefunction", fieldChanged, "hotpink");
    }
}

我将使用的原始答案

?操作员制作简单的HTML而没有其他代码

<input type="text" placeholder="First Name" id=FirstName @bind=@textInput style='@(String.IsNullOrEmpty(textInput) ? "background-color: hotpink;" : "")' />

Revised Answer

Or you can use JSInterop like this:

/wwwroot/js/example.js

export function examplefunction (Id, Color) {
    document.getElementById(Id).style.backgroundColor = Color;
}

razor page :

@inject IJSRuntime JS

@code {
    private IJSObjectReference? module;
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            module = await JS.InvokeAsync<IJSObjectReference>("import", "/js/example.js");
        }
    }
    private async void UpdateVisibleData(object sender, FieldChangedEventArgs e){
        var fieldChanged = e.FieldIdentifier.FieldName;
        await module.InvokeVoidAsync("examplefunction", fieldChanged, "hotpink");
    }
}

Original Answer

I would use ? operator to make simple HTML without additional code

<input type="text" placeholder="First Name" id=FirstName @bind=@textInput style='@(String.IsNullOrEmpty(textInput) ? "background-color: hotpink;" : "")' />
凌乱心跳 2025-02-14 02:17:35

这不是针对ID的确切答案。但是,我确实找到了一个解决方法,这对于WebAssembly来说还不错,通过将条件功能传递给元素样式标签。它不会像使用其他障碍并声明单个字符串那样增加代码库。尽管仍基于fieldEventChanges的元素ID仍将征收计算征税,因为在每个字段上检查了每个字段的条件函数,而不是仅在满足条件时仅在元素ID上调用元素。

html

<th><InputText style="@StyleForEmptyString(filterModel.FirstName)" placeholder="First Name" id=FirstName @bind-Value="@filterModel.FirstName">First Name</InputText></th>

@code {

private string StyleForEmptyString(string fieldValue)
    {
        if (fieldValue == ""){
            return "outline:none; box-shadow:none;";
        }
        return "";
    }```

This isn't a exact answer by targeting the id. however I did find a workaround for this which isn't too bad for webassembly by passing a conditional function to the style tag on the element. It doesn't grow the code base as much as using if else blocks and declaring individual strings. although still targeting element id based on fieldeventchanges would be less computationally taxing as the conditional function is checked for every statechange on every field which uses it as opposed to only being called on the element id if the condition is met.

html

<th><InputText style="@StyleForEmptyString(filterModel.FirstName)" placeholder="First Name" id=FirstName @bind-Value="@filterModel.FirstName">First Name</InputText></th>

@code{

private string StyleForEmptyString(string fieldValue)
    {
        if (fieldValue == ""){
            return "outline:none; box-shadow:none;";
        }
        return "";
    }```
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文