簡體   English   中英

無法使用 JS 獲取元素的“寬度”屬性

[英]Can't get 'width' attribute of an element with JS

我無法獲得元素的“寬度”屬性:

 console.log(document.querySelector('.container').width)
 <div class="container" width="55"> </div>

這將返回“未定義”,其他屬性如“id”我可以通過這種方式獲得,但不是寬度和高度。 我覺得這個問題很愚蠢,為什么它不起作用? 即使在這里他們寫它應該工作: https://www.w3schools.com/jsref/prop_object_width.asp

我不需要任何解決方法,例如 jQuery 或 outerWidth 等。我只需要從元素中獲取寬度和高度屬性

編輯:我的實際問題是 SVG 元素,但使用 DIV 它也不起作用。

您正在尋找Element.getAttribute()文檔

 console.log(document.querySelector('.container').getAttribute('width'))
 <div class="container" width="55"></div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM