首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取错误的.clientHeight值

问获取错误的.clientHeight值
EN

Stack Overflow用户
提问于 2021-02-14 04:35:44
回答 1查看 214关注 0票数 0

这里有相当多的帖子,但没有一个建议的想法对我有效。我只是想在页面渲染后获得一个灵活元素div的正确高度。通过let div = document.querySelector("#cbody");获取div给了我

代码语言:javascript
复制
div#cbody.card-body
​
accessKey: ""
​
accessKeyLabel: ""
​
align: ""
​
assignedSlot: null
​
attributes: NamedNodeMap [ class="card-body", id="cbody" ]
​
baseURI: "http://matpro/toolplan"
​
childElementCount: 2
​
childNodes: NodeList(5) [ #text, div.toolbar, #text
, … ]
​
children: HTMLCollection { 0: div.toolbar, 1: div#calendar.fc.fc-media-screen.fc-direction-ltr.fc-theme-standard.fc-liquid-hack
, length: 2, … }
​
classList: DOMTokenList [ "card-body" ]
​
className: "card-body"
​
clientHeight: 1201
​
clientLeft: 1
​
clientTop: 1
​
clientWidth: 1221
​
contentEditable: "inherit"
​
dataset: DOMStringMap(0)
​
dir: ""
​
draggable: false
​
firstChild: #text "  
            "​
firstElementChild: <div class="toolbar" style="border:0px solid coral;">
​
hidden: false
​
id: "cbody"
​
innerHTML: "  \n            <div class=\"toolbar\" style=\"border:0px solid coral;\"></div>\n            <div id=\"calendar\" style=\"border:3px solid red;\" class=\"fc fc-media-screen fc-direction-ltr fc-theme-standard fc-liquid-hack\"><div class=\"fc-header-toolbar fc-toolbar fc-toolbar-ltr\"><div class=\"fc-toolbar-chunk\"><h2 class=\"fc-toolbar-title\">08.02.2021 – 04.04.2021</h2></div><div class=\"fc-toolbar-chunk\"><div class=\"fc-button-group\"><button class=\"fc-week-button fc-button fc-button-primary\" type=\"button\">Woche</button><button class=\"fc-month-button fc-button fc-button-primary fc-button-active\" type=\"button\">2 Monate</button></div></div><div class=\"fc-toolbar-chunk\"><button class=\"fc-cToday-button fc-button fc-button-primary\" type=\"button\">Heute</button><div class=\"fc-button-group\"><button class=\"fc-prevMonth-button fc-button fc-button-primary\" type=\"button\">&lt;&lt;</button><button class=\"fc-prevWeek-button fc-button fc-button-primary\" type=\"button\">&lt;</button><button class=\"fc-nextWeek-button fc-b…"
​
innerText: "08.02.2021 – 04.04.2021\nWoche\n2 Monate\nHeute\n<<\n<\n>\n>>\nRessource\nsort_by_alpha\n\t\t\nFebruar 2021\n\t\nMärz 2021\n\t\nApril 2021\n\n\n6\n\t\n7\n\t\n8\n\t\n9\n\t\n10\n\t\n11\n\t\n12\n\t\n13\n\n\nMo., 08.\n\t\nDi., 09.\n\t\nMi., 10.\n\t\nDo., 11.\n\t\nFr., 12.\n\t\nSa., 13.\n\t\nSo., 14.\n\t\nMo., 15.\n\t\nDi., 16.\n\t\nMi., 17.\n\t\nDo., 18.\n\t\nFr., 19.\n\t\nSa., 20.\n\t\nSo., 21.\n\t\nMo., 22.\n\t\nDi., 23.\n\t\nMi., 24.\n\t\nDo., 25.\n\t\nFr., 26.\n\t\nSa., 27.\n\t\nSo., 28.\n\t\nMo., 01.\n\t\nDi., 02.\n\t\nMi., 03.\n\t\nDo., 04.\n\t\nFr., 05.\n\t\nSa., 06.\n\t\nSo., 07.\n\t\nMo., 08.\n\t\nDi., 09.\n\t\nMi., 10.\n\t\nDo., 11.\n\t\nFr., 12.\n\t\nSa., 13.\n\t\nSo., 14.\n\t\nMo., 15.\n\t\nDi., 16.\n\t\nMi., 17.\n\t\nDo., 18.\n\t\nFr., 19.\n\t\nSa., 20.\n\t\nSo., 21.\n\t\nMo., 22.\n\t\nDi., 23.\n\t\nMi., 24.\n\t\nDo., 25.\n\t\nFr., 26.\n\t\nSa., 27.\n\t\nSo., 28.\n\t\nMo., 29.\n\t\nDi., 30.\n\t\nMi., 31.\n\t\nDo., 01.\n\t\nFr., 02.\n\t\nSa., 03.\n\t\nSo., 04.\n\n\nAimo-Clemens Leffers (0551)\nedit\n\n\nAlex Rabe (0724)\nedit\n\n\nAlfred Winter (0583)\nedit\n\n\nAljoscha Raschke (0874)\nedit\n\n\nAndre Elberling (0587)\nedit\n\n\nAndre Michels (0714)\nedit\n\n\nAndreas Milczewski (0575)\nedit\n\n\nAnhänger (BRA-AW 429)\nedit\n\n\nAnhänger (FRI-LA 103)\nedit\n\n\nAnhänger (FRI-LA 51)\nedit\n\n\nAnhänger (FRI-LA 80)\nedit\n\n\nAstra (FRI-LA 144)\nedit\n\n\nBaggeranhänger (FRI-LA 50)\nedit\n\n\nBastian Müller (0510)\nedit\n\n\nBenjamin Umbach (0578)\nedit\n\n\nBennet Dreemann (0876)\nedit\n\n\nBennet Visser (0911)\nedit\n\n\nCaddy (BRA-SE 103)\nedit\n\n\nCaddy (FRI-LA 20)\nedit\n\n\nCorinna Wessels (0415)\nedit\n\n\nDaniel Powollik (0593)\nedit\n\n\nHeiko Wessels (0679)\nedit\n\n\nMustermann (5559)\nedit\n\n\nTestRessource (1234)\nedit\n\t\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\nKarfreitag\nRosenmontag\nFastnacht\nAschermittwoch\nPalmsonntag\nGründonnerstag\nOstersonntag\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
​
isConnected: true
​
isContentEditable: false
​
lang: ""
​
lastChild: #text "
          "​
lastElementChild: <div id="calendar" class="fc fc-media-screen fc-di…standard fc-liquid-hack" style="border:3px solid red;">
​
localName: "div"
​
namespaceURI: "http://www.w3.org/1999/xhtml"
​
nextElementSibling: null
​
nextSibling: #text "
        "
​
nodeName: "DIV"
​
nodeType: 1
​
nodeValue: null
​
nonce: ""
​
offsetHeight: 1203
​
offsetLeft: 0
​
offsetParent: <div class="card" style="border:0px solid coral;padding-bottom:5px;">
​
offsetTop: 47
​
offsetWidth: 1223
​
onabort: null
​
onanimationcancel: null
​
onanimationend: null
​
onanimationiteration: null
​
onanimationstart: null
​
onauxclick: null
​
onblur: null
​
oncanplay: null
​
oncanplaythrough: null
​
onchange: null
​
onclick: null
​
onclose: null
​
oncontextmenu: null
​
oncopy: null
​
oncuechange: null
​
oncut: null
​
ondblclick: null
​
ondrag: null
​
ondragend: null
​
ondragenter: null
​
ondragexit: null
​
ondragleave: null
​
ondragover: null
​
ondragstart: null
​
ondrop: null
​
ondurationchange: null
​
onemptied: null
​
onended: null
​
onerror: null
​
onfocus: null
​
onformdata: null
​
onfullscreenchange: null
​
onfullscreenerror: null
​
ongotpointercapture: null
​
oninput: null
​
oninvalid: null
​
onkeydown: null
​
onkeypress: null
​
onkeyup: null
​
onload: null
​
onloadeddata: null
​
onloadedmetadata: null
​
onloadend: null
​
onloadstart: null
​
onlostpointercapture: null
​
onmousedown: null
​
onmouseenter: null
​
onmouseleave: null
​
onmousemove: null
​
onmouseout: null
​
onmouseover: null
​
onmouseup: null
​
onmozfullscreenchange: null
​
onmozfullscreenerror: null
​
onpaste: null
​
onpause: null
​
onplay: null
​
onplaying: null
​
onpointercancel: null
​
onpointerdown: null
​
onpointerenter: null
​
onpointerleave: null
​
onpointermove: null
​
onpointerout: null
​
onpointerover: null
​
onpointerup: null
​
onprogress: null
​
onratechange: null
​
onreset: null
​
onresize: null
​
onscroll: null
​
onseeked: null
​
onseeking: null
​
onselect: null
​
onselectstart: null
​
onstalled: null
​
onsubmit: null
​
onsuspend: null
​
ontimeupdate: null
​
ontoggle: null
​
ontransitioncancel: null
​
ontransitionend: null
​
ontransitionrun: null
​
ontransitionstart: null
​
onvolumechange: null
​
onwaiting: null
​
onwebkitanimationend: null
​
onwebkitanimationiteration: null
​
onwebkitanimationstart: null
​
onwebkittransitionend: null
​
onwheel: null
​
outerHTML: "<div class=\"card-body\" id=\"cbody\">  \n            <div class=\"toolbar\" style=\"border:0px solid coral;\"></div>\n            <div id=\"calendar\" style=\"border:3px solid red;\" class=\"fc fc-media-screen fc-direction-ltr fc-theme-standard fc-liquid-hack\"><div class=\"fc-header-toolbar fc-toolbar fc-toolbar-ltr\"><div class=\"fc-toolbar-chunk\"><h2 class=\"fc-toolbar-title\">08.02.2021 – 04.04.2021</h2></div><div class=\"fc-toolbar-chunk\"><div class=\"fc-button-group\"><button class=\"fc-week-button fc-button fc-button-primary\" type=\"button\">Woche</button><button class=\"fc-month-button fc-button fc-button-primary fc-button-active\" type=\"button\">2 Monate</button></div></div><div class=\"fc-toolbar-chunk\"><button class=\"fc-cToday-button fc-button fc-button-primary\" type=\"button\">Heute</button><div class=\"fc-button-group\"><button class=\"fc-prevMonth-button fc-button fc-button-primary\" type=\"button\">&lt;&lt;</button><button class=\"fc-prevWeek-button fc-button fc-button-primary\" type=\"button\">&lt;</button><but…"
​
ownerDocument: HTMLDocument http://matpro/toolplan
​
parentElement: <div class="card" style="border:0px solid coral;padding-bottom:5px;">​
parentNode: <div class="card" style="border:0px solid coral;padding-bottom:5px;">​
part: DOMTokenList []
​
prefix: null
​
previousElementSibling: <div class="row" style="border:0px solid blue;">​
previousSibling: #text " 
          "
​
scrollHeight: 1201
​
scrollLeft: 0
​
scrollLeftMax: 0
​
scrollTop: 0
​
scrollTopMax: 0
​
scrollWidth: 1221
​
shadowRoot: null
​
slot: ""
​
spellcheck: false
​
style: CSS2Properties(0)
​
tabIndex: -1
​
tagName: "DIV"
​
textContent: "  \n            \n            08.02.2021 – 04.04.2021Woche2 MonateHeute<<<>>>Ressourcesort_by_alphaFebruar 2021März 2021April 2021678910111213Mo., 08.Di., 09.Mi., 10.Do., 11.Fr., 12.Sa., 13.So., 14.Mo., 15.Di., 16.Mi., 17.Do., 18.Fr., 19.Sa., 20.So., 21.Mo., 22.Di., 23.Mi., 24.Do., 25.Fr., 26.Sa., 27.So., 28.Mo., 01.Di., 02.Mi., 03.Do., 04.Fr., 05.Sa., 06.So., 07.Mo., 08.Di., 09.Mi., 10.Do., 11.Fr., 12.Sa., 13.So., 14.Mo., 15.Di., 16.Mi., 17.Do., 18.Fr., 19.Sa., 20.So., 21.Mo., 22.Di., 23.Mi., 24.Do., 25.Fr., 26.Sa., 27.So., 28.Mo., 29.Di., 30.Mi., 31.Do., 01.Fr., 02.Sa., 03.So., 04.Aimo-Clemens Leffers (0551) editAlex Rabe (0724) editAlfred Winter (0583) editAljoscha Raschke (0874) editAndre Elberling (0587) editAndre Michels (0714) editAndreas Milczewski (0575) editAnhänger (BRA-AW 429) editAnhänger (FRI-LA 103) editAnhänger (FRI-LA 51) editAnhänger (FRI-LA 80) editAstra (FRI-LA 144) editBaggeranhänger (FRI-LA 50) editBastian Müller (0510) editBenjamin Umbach (0578) editBennet Dreemann (0876) editBennet Visser (0911) editCaddy (BRA-SE 103) editCaddy (FRI-LA 20) editCorinna Wessels (0415) editDaniel Powollik (0593) editHeiko Wessels (0679) editMustermann (5559) editTestRessource (1234) editKarfreitagRosenmontagFastnachtAschermittwochPalmsonntagGründonnerstagOstersonntag\n          "
​
title: ""
​
<prototype>: HTMLDivElementPrototype { align: Getter & Setter, … }

所以clientHeight显然是1201。火狐的检查员也给了我正确的信息div#cbody.card-body | 1223 x 1202.75 | Flex-Element。然而,下面获得这个高度的代码直接给出了错误的值219。不过,我可以为clientWidth获得正确的值:

代码语言:javascript
复制
$(document).ready(function() {
    let h = document.getElementById("cbody").clientHeight; 
    console.log(h);  // 219
    console.log(div.scrollHeight); // 219
    console.log(div.clientHeight); // 219
    console.log(div.offsetHeight); // 221
    console.log($('#cbody').height()); // 188.75

    let w = document.getElementById("cbody").clientWidth;
    console.log(w);  // 1221, works!
});

这是页面:

代码语言:javascript
复制
<div class="content" style="padding-bottom:0px;">  
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-12">
        <div class="card" style="padding-bottom:5px;">              
          <div class="card-header card-header-danger card-header-icon">
            <div class="card-icon"><i class="material-icons">person</i></div>
            <h4 class="card-title">{{ __('Calendar') }}</h4>
          </div>
          <div class="row">
            <div class="col-12 text-right"></div>
          </div> 
          <div class="card-body" id="cbody">  
            <div class="toolbar"></div>
            <div id="calendar"></div>
          </div>
        </div>
      </div>          
    </div>      
  </div>
</div>

我遗漏了什么?

EN

回答 1

Stack Overflow用户

发布于 2021-02-14 05:30:22

我的直觉告诉我,这可能与你试图访问DOM的生命周期方法有关。

您的事件可能需要从$(document).ready更改为$(window).load才能正常工作。这是因为cbody元素的高度似乎是由它的子元素的内容控制的,这些子元素在默认情况下看起来是空的,但后来(但在加载之前)被填充了。检查时看到的高度是加载子元素内容时的最终高度。

ready事件在构建初始DOM结构时触发。

稍后,当包括图像、样式和其他资源在内的所有资源也被加载时,将触发load事件。

另一个使用getComputedStyle的调试尝试:-

代码语言:javascript
复制
let cbody = document.querySelector("#cbody")
console.log(getComputedStyle(cbody).getPropertyValue('height'));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66189654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档