通过针对html元素的设置样式的html元素。
说我有一个元素。我想根据《大火》中输入字段的值设置元素样式。我知道您可以通过使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
修订后的答案
,或者您可以这样使用jsinterop:
/wwwroot/js/example.js
剃须刀页面:
我将使用的原始答案
?操作员制作简单的HTML而没有其他代码
Revised Answer
Or you can use JSInterop like this:
/wwwroot/js/example.js
razor page :
Original Answer
I would use ? operator to make simple HTML without additional code
这不是针对ID的确切答案。但是,我确实找到了一个解决方法,这对于WebAssembly来说还不错,通过将条件功能传递给元素样式标签。它不会像使用其他障碍并声明单个字符串那样增加代码库。尽管仍基于fieldEventChanges的元素ID仍将征收计算征税,因为在每个字段上检查了每个字段的条件函数,而不是仅在满足条件时仅在元素ID上调用元素。
html
@code {
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
@code{