Best way to check an element's width?

Mad Echet

Will checking the width attribute be sufficient to make sure my DOM element has a striclty positive width or should I also check for css rules (I am only looking for elements that have a fixed number of pixels as a CSS rule, no % width).

if (element.offsetWidth && element.offsetWidth > 0) // do stuff

OR

if ((element.offsetWidth && element.offsetWidth > 0)
     || (element.style && element.style.width
         && int(element.style.width.replace("px", ""), 10)>0 ) // do stuff

I would rather go for the first one but I want to make sure that setting the CSS rule will also set the width attribute.

---- EDIT ----

Actually offsetWidth seems to be better than width in any case. I changed width to offsetWidth.

Does it make the second option (checking for the CSS) worthless?

SmokeyPHP

element.offsetWidth should suffice - and no, checking the style element will not be necessary

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

The best way to check selector in Jsoup

분류에서Dev

What's the best way to sort?

분류에서Dev

Best way to count unique element in a string in r

분류에서Dev

What's the best way to list size of / in Terminal?

분류에서Dev

Android what's the best way to hide logs?

분류에서Dev

What is the best way to check if a User has access to an Entity

분류에서Dev

Best way to get model associated with a core-item element?

분류에서Dev

What's the best way to get Python 2.5 and 2.7

분류에서Dev

What's the best way to grab attributes of a particular td in html table?

분류에서Dev

Best way to Duplicate a Laptop's Hard Drive One-to-One

분류에서Dev

What's the best way to pass a huge collection to a Spring Batch Step?

분류에서Dev

What's the Best Way to Write Text to Blob in an Azure Webjob

분류에서Dev

What's the best way to transform a string into an associative array

분류에서Dev

Check for window width or height

분류에서Dev

Clever way to predict width

분류에서Dev

What's the easiest way to target a specific block level element

분류에서Dev

WPF - Is there a way to target an element type in a ControlTemplate's trigger?

분류에서Dev

Best way to optimize this function

분류에서Dev

Best way to design this code?

분류에서Dev

Why does the test pass when using the element? method to check the element's existence?

분류에서Dev

pygame.error: Text has zero width; what's a better way to make it work?

분류에서Dev

What's the best way to make a program update from a new PPA (when an old one is no longer maintained)?

분류에서Dev

AngularJS - What's the best way to implement a "picture-in-picture" view within my stated app?

분류에서Dev

What's the best way to call a php method in zend model using javascript/jQuery

분류에서Dev

What's the best way to authorize a back-end sever to use the google drive api?

분류에서Dev

What is the best way to uninstall nginx

분류에서Dev

Best way to offer help documentation

분류에서Dev

What is the best way of doing multithreading?

분류에서Dev

Best way to share an image on Facebook?

Related 관련 기사

  1. 1

    The best way to check selector in Jsoup

  2. 2

    What's the best way to sort?

  3. 3

    Best way to count unique element in a string in r

  4. 4

    What's the best way to list size of / in Terminal?

  5. 5

    Android what's the best way to hide logs?

  6. 6

    What is the best way to check if a User has access to an Entity

  7. 7

    Best way to get model associated with a core-item element?

  8. 8

    What's the best way to get Python 2.5 and 2.7

  9. 9

    What's the best way to grab attributes of a particular td in html table?

  10. 10

    Best way to Duplicate a Laptop's Hard Drive One-to-One

  11. 11

    What's the best way to pass a huge collection to a Spring Batch Step?

  12. 12

    What's the Best Way to Write Text to Blob in an Azure Webjob

  13. 13

    What's the best way to transform a string into an associative array

  14. 14

    Check for window width or height

  15. 15

    Clever way to predict width

  16. 16

    What's the easiest way to target a specific block level element

  17. 17

    WPF - Is there a way to target an element type in a ControlTemplate's trigger?

  18. 18

    Best way to optimize this function

  19. 19

    Best way to design this code?

  20. 20

    Why does the test pass when using the element? method to check the element's existence?

  21. 21

    pygame.error: Text has zero width; what's a better way to make it work?

  22. 22

    What's the best way to make a program update from a new PPA (when an old one is no longer maintained)?

  23. 23

    AngularJS - What's the best way to implement a "picture-in-picture" view within my stated app?

  24. 24

    What's the best way to call a php method in zend model using javascript/jQuery

  25. 25

    What's the best way to authorize a back-end sever to use the google drive api?

  26. 26

    What is the best way to uninstall nginx

  27. 27

    Best way to offer help documentation

  28. 28

    What is the best way of doing multithreading?

  29. 29

    Best way to share an image on Facebook?

뜨겁다태그

보관