【问题标题】:how to set multiple CSS style properties in typescript for an element?如何在 typescript 中为一个元素设置多个 CSS 样式属性?
【发布时间】:2016-10-05 22:35:12
【问题描述】:

请考虑以下 sn-p。我需要在打字稿中设置多个 CSS 属性。为此,我尝试了以下代码。

public static setStyleAttribute(element: HTMLElement, attrs: { [key: string]: Object }): void {
        if (attrs !== undefined) {
            Object.keys(attrs).forEach((key: string) => {
                element.style[key] = attrs[key];
            });
        }
    }

对于上述代码,我需要将参数传递为

let elem: HTMLElement = document.getElementById('myDiv');
setStyleAttribute(elem, {font-size:'12px', color : 'red' , margin-top: '5px'});

但上面的代码会抛出错误(tslint),因为 Element 隐含地具有“任何”类型,因为索引表达式不是“数字”类型。 (属性) HTMLElement.style: CSSStyleDeclaration.

请帮帮我!!!

【问题讨论】:

  • 这一行中抛出的错误............ element.style[key]

标签: css typescript tslint


【解决方案1】:

我希望这可以帮助你或其他人......

您可以使用HTLMDivElement 和其中包含的CSSStyleDeclaration 来实现此目的。例如。

var container: HTMLDivElement;

container.style.color = "red";
container.style.fontSize = "12px";
container.style.marginTop = "5px";

这也适用于从HTMLElement 继承并具有style 属性的其他类(例如HTMLBodyElement

【讨论】:

  • 可能适用于某些人。不适合我的场景..:)
【解决方案2】:

尝试使用setAttribute。 TypeScript 在Element 上没有style 属性。

element.setAttribute("style", "color:red; border: 1px solid blue;");

此 GitHub 问题中的一些相关讨论: https://github.com/Microsoft/TypeScript/issues/3263

【讨论】:

  • 这段代码有副作用,它会杀死所有旧样式定义。
  • 这是真的!您需要使用 element.getAttribute('style') 并将两者合并在一起以保持现有样式。
【解决方案3】:

聚会晚了一点,但无论如何......

实际问题不在于style 没有在Element 上定义。 Element implicitly has an 'any' type because index expression is not of type 'number'. (property) HTMLElement.style: CSSStyleDeclaration 开头的单词Element 只是句子中的第一个单词,因此大写。意味着它与 Element 对象没有任何关系——这很令人困惑。

但是,错误消息意味着您正尝试使用下标运算符 [] 访问属性,但索引类型不正确。在您的情况下,您的keystring 类型,但HTMLElement.style 是一个CSSStyleDeclaration 对象,其索引签名为[index: number]: string,因此需要您的密钥类型为number

索引签名来自 TypeScript 安装中的 typescript/lib/lib.dom.d.ts 声明文件。在那里你会找到CSSStyleDeclaration

所以你可以做的就是像这样简单地转换为any

(<any>element.style)[key] = attr[key];

这不是最好的解决方案,但它有效且简单明了。它也不需要您像使用 element.setAttribute 时那样对样式进行字符串化。

【讨论】:

  • 顺便说一句:有谁知道为什么 TypeScript 声明文件中的 CSSStyleDeclaration 具有基于数字的索引签名而不是基于字符串的索引签名?对我来说毫无意义。
  • 它反映了浏览器。尝试:myElement.style[0] 访问 myElement 的第一个样式。
  • 这里是github上的相关问题github.com/microsoft/TypeScript/issues/17827
【解决方案4】:

您要搜索的 API 是:https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/setProperty

public static setStyleAttribute(element: HTMLElement, attrs: { [key: string]: Object }): void {
    if (attrs !== undefined) {
        Object.keys(attrs).forEach((key: string) => {
            element.style.setProperty(key, attrs[key]);
        });
    }
}

并且对象键中不允许使用连字符,所以在这里也使用 ':

let elem: HTMLElement = document.getElementById('myDiv');
setStyleAttribute(elem, {'font-size':'12px', color: 'red', 'margin-top': '5px'});

【讨论】:

  • 这应该是公认的答案。 TypeScript 的 CSSStyleDeclaration 具有基于数字的索引(在此处的其他答案的 cmets 中引用),因此字符串键不起作用。 setProperty 接受字符串键并且与style[key] = val 具有相同的效果,而无需使用不安全的输入。谢谢@HolgerJeromin
【解决方案5】:

一种不安全/无类型的方式:

const e = document.createElement("div")
Object.assign(e.style, {borderSize: "1rem", verticalAlign: "middle"})

您需要将标准 CSS 样式命名转换为 TypeScript 替代方案才能正常工作。即font-sizefontSize。请注意:您不会收到错误样式名称的错误。 {attrFOO: "bar"} 有效。

【讨论】:

    【解决方案6】:

    您可以将其用于单个元素,并对其进行迭代以使其成为动态

    let el: HTMLElement = document.getElementById('elementID');
    
    el.style.height = "500px";
    

    【讨论】:

    • 我已经将它与 TypeScript / Angular 一起使用。通常,document.getElementById() 可以作为Element 类型返回,请确保使用let el: HTMLElement = &lt;HTMLElement&gt;document.getElementById('elementID'); 进行更改。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2010-11-01
    • 2011-05-15
    • 1970-01-01
    • 2014-07-15
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    相关资源
    最近更新 更多