Get actual width and height of a component

前端 未结 7 982
离开以前
离开以前 2021-02-12 22:49

We\'re facing a fairly scary issue in JavaScript that none of us seems to be quite capable of resolving:

How do we get the width and height of a DOM element, including

7条回答
  •  你的背包
    2021-02-12 23:23

    The problem here is you can't get the element size before you add this element on the page.

    On your method "constructElement", you adding the element to a root element, but just on onload you adding to the page. Your property "rootElement" not have the size properties setted.

    Rethink the structure of your object/method if you want to manipulate these properties.

    Look my update of your fiddle

    $(function ()
    {
        var ex = new exampleElement("Hello", "Here's text");
        ex.constructElement();
        document.body.appendChild(ex.rootElement);
    
        $("")
            .text(" => Width is " + $("div").width() + ", and Height is " + $("div").height())
            .appendTo($("div p"));
    });
    

提交回复
热议问题