element.setAttribute('style', 'attribute :value;') vs. element.attribute = 'value'

前端 未结 2 2149
你的背包
你的背包 2020-12-15 17:59

In javascript is there any difference between using

element.style.setAttribute(\'width\', \'150px\');

and

element.style.wi         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 18:58

    Both are perfectly valid. Can you give some examples which doesn't work in second way? Are you aware that attribute names needs to be camelcased first? E.g. element.style.marginTop instead of incorrectly element.style.margin-top. The hyphen is namely an invalid identifier in Javascript.

提交回复
热议问题