广州响应式网站,怎么创建图片网站,柳州中小企业网站建设,codeus wordpress原文链接#xff1a;https://blazor-university.com/components/code-generated-html-attributes/代码生成 HTML 属性Razor 在条件 HTML 输出或在 for 循环中输出 HTML 时非常棒#xff0c;但在元素本身内的条件代码方面#xff0c;事情就有点棘手了。例如#xff0c;以下代… 原文链接https://blazor-university.com/components/code-generated-html-attributes/代码生成 HTML 属性Razor 在条件 HTML 输出或在 for 循环中输出 HTML 时非常棒但在元素本身内的条件代码方面事情就有点棘手了。例如以下代码无法编译因为您无法在元素的 和 内添加 C# 控制块。imgforeach(var nameAndValue in AdditionalAttributes){nameAndValue.Key nameAndValue.Value} srchttps://randomuser.me/api/portraits/lego/1.jpg /code
{Dictionarystring, object AdditionalAttributes;protected override void OnInitialized(){AdditionalAttributes new Dictionarystring, object{[id] EmmetImage,[alt] A photo of Emmet};base.OnInitialized();}
}我们可能尝试的下一个方法是编写一个返回字符串的方法并在 和 字符内调用它。div IfYouCanSeeThisTextThenTheCodeWasNotExecutedHere /
spanIfYouCanSeeThisTextThenTheCodeWasNotExecutedHere/spancode
{string IfYouCanSeeThisTextThenTheCodeWasNotExecutedHere The code here was executed;
}但这也不起作用。前面的示例将输出以下 HTML。div ifyoucanseethistextthenthecodewasnotexecutedhere/div
spanThe code here was executed/spanRazor 只会在以下位置执行 C# 代码在元素的内容区域内例如 spanGetSomeHtml()/span。在确定要分配给元素属性的值时例如 img srcGetTheImageForTheUrl() /。在 code 部分中。我们需要用来为 HTML 元素生成一个或多个属性 值的技术称为“属性展开”。属性展开涉及将 Dictionarystring, object 分配给具有特殊名称 attribute 的属性。div attributesMyCodeGeneratedAttributes/code
{Dictionarystring, object MyCodeGeneratedAttributes;protected override void OnInitialized(){MyCodeGeneratedAttributes new Dictionarystring, object();for(int index 1; index 5; index){MyCodeGeneratedAttributes[attribute_ index] index;}}
}前面的代码将输出一个具有 5 个属性的 div。div attribute_11 attribute_22 attribute_33 attribute_44 attribute_55/div特殊情况一些 HTML 属性例如 readonly 和 disabled 不需要任何值——它们仅存在于元素上就足以使它们生效。事实上即使应用诸如 false 之类的值仍然会激活它们。以下 input 元素将是只读和禁用的。input readonlyfalse disbabledfalse/在 razor 视图中规则略有不同。如果我们输出 readonlyIsReadOnly 或 disabledIsDisabled - 只要分配的值为 falserazor 根本不会输出该属性当分配的值为 true 时razor 将在不分配值的情况下输出元素。input readonlytrue disabledfalse/ 将导致生成的 HTML 完全不包含 disabled 属性。
相关文章: