How do I reference a javascript object property with a hyphen in it?

后端 未结 11 745
慢半拍i
慢半拍i 2020-11-22 06:07

Using this script to make a style object of all the inherited etc styles.

var style = css($(this));
alert (style.width);
alert (style.text-align);

11条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 06:26

    Use brackets:

    var notTheFlippingStyleObject = {
        'a-b': 1
    };
    
    console.log(notTheFlippingStyleObject["a-b"] === 1); // true
    

    More info on objects: MDN

    NOTE: If you are accessing the style object, CSSStyleDeclaration, use must camelCase to access it from javascript. More info here

提交回复
热议问题