element.setAttribute('style','attribute:value;')vs. element.attribute ='value' [英] element.setAttribute('style', 'attribute :value;') vs. element.attribute = 'value'
问题描述
element.style.setAttribute('width','150px');
和
element.style.width ='150px';
?
我已经看到关键字不会以第一种方式工作(如 this ),但是对于非关键字属性是否有区别?
两者都是完全有效的。你能举出一些不适用于第二种方式的例子吗?您是否知道属性名称必须先被 element.style.marginTop
而不是错误的 element.style.margin-top
。连字符即是Javascript中的无效标识符。
In javascript is there any difference between using
element.style.setAttribute('width', '150px');
and
element.style.width = '150px';
?
I have seen that keywords won't work with the first way (like this), but for non-keyword attributes is there a difference?
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.
这篇关于element.setAttribute('style','attribute:value;')vs. element.attribute ='value'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!