Different ways of accessing attribute values using javascript

前端 未结 4 901
生来不讨喜
生来不讨喜 2020-12-14 18:02
document.getElementById(\'myId\').style;

is one way of accessing the style attribute.. Also we can do the same using document.getElementById

4条回答
  •  抹茶落季
    2020-12-14 18:30

    there is no difference.

    document.getElementById('myId').style;
    

    is a shorthand for

    document.getElementById('myId').getAttribute('style');
    

    the only use for getAttribute('attributeName') would be if attributeName is not a valid javascript variable name, so encapsulating it in quotes would be the only way to access it.

提交回复
热议问题