2017-09-24 3 views
0

Hier ist das Problem, ich kann nicht Breite von <ul> bekommen.Breite von UL erhalten

html-Datei:

<ul> 
    <li>image goes here</li> 
    <li>image goes here</li> 
    <li>image goes here</li> 
</ul> 

CSS-Datei:

ul { 
    display: inline-block; 
    white-space: nowrap; 
} 

li { 
    width: 90px; 
    height: 90px; 
    display: inline-block; 
} 

hier ist Beispiel für JQuery-Code:

$(document).ready(function(){ 
    var $width = $('ul').width(); 
    console.log('width: ' + $width); 
}); 

es gibt 0 zurück, wie ul Breite zu bekommen?

Antwort

0

Verwenden Sie die CSS Funktion css mit wdith als Attribut:

$('ul').css("width"); 

$(document).ready(function() { 
 
    var $width = $('ul').css("width"); 
 
    console.log('width: ' + $width); 
 
});
ul { 
 
    display: inline-block; 
 
    white-space: nowrap; 
 
} 
 

 
li { 
 
    width: 90px; 
 
    height: 90px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<ul> 
 
    <li>image goes here</li> 
 
    <li>image goes here</li> 
 
    <li>image goes here</li> 
 
</ul>

+0

i vergessen 'display: inline-block' auf li. –

+0

@BatgerelEnkhbat meine Lösung funktioniert auch ^^ aber ich bin froh, dass Sie finden, was Sie verpasst viel Glück –