首页 > 解决方案 > 使用 TagBuilder 类 C# 插入属性

问题描述

我想知道是否有一种方法可以在不使用 TagBuilder 类的 MergeAttributes 或 MergeAttribute 方法的情况下在脚本标记中插入属性。

我有一个属性数据列表:

AttributeDataList = new List<AttributeData>
{
    new AttributeData()
    {
        Key = "",
        Value = ""
    }
},

在我负责创建脚本的助手中,我进行了以下检查:

if (script.AttributeDataList.Count > 0)
{
    foreach (var attr in script.AttributeDataList)
    {
        scriptTag.MergeAttribute(attr.Key, attr.Value);
    }
}

现在假设我想插入“async”或“defer”属性而不使用“async=async”,我该怎么做?TagBuilder 类似乎没有提供这样的方法,我得到的是async=""

我正在考虑使用正则表达式,并将“=”替换为“”,但这种模式@"=\\"""似乎不起作用。

有解决方案吗?非常感谢

标签: c#script-tag

解决方案


推荐阅读