2016-09-26 64 views
0

我有一個引導模式,其編號爲myModalclientWidth的自舉模態返回0

在我的javascript當我這樣做

window.onload = function(){ 
     var container = document.getElementById("myModal"); 
     console.log(container.clientWidth); 
} 

它顯示0。 如何在Bootstrap模式打開之前獲取clientWidth?

+0

你能分享可執行的演示/片段或[JSFiddle](https://jsfiddle.net/)嗎? [_創建一個最小,完整和可驗證的示例_](http://stackoverflow.com/help/mcve) – Rayon

回答

0

jQuery的width工作得很好

$('#ModalName').find('.modal-dialog').width() 

對於$('#ModalName')你總是得到錯誤的值。

+0

那麼你知道些什麼。 jQuery的作品。但任何想法爲什麼純JS客戶端的寬度不工作? –

+0

在[這篇文章](http://stackoverflow.com/questions/294250/how-do-i-retrieve-an-html-elements-actual-width-and-height)有關於評論中同樣的問題的討論爲接受答案。也許它對你有用。 – user3272018

+0

更多詳細信息[here](http://stackoverflow.com/questions/1841124/find-the-potential-width-of-a-hidden-element)和[here](http://stackoverflow.com/questions/ 30637577 /如何對獲得-clientwidth-clientheight前方的DIV-是可見的)。 – user3272018