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

查看:112
本文介绍了element.setAttribute('style','attribute:value;')vs. element.attribute ='value'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中使用

  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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆