重複の可能性:
要素の幅を px で取得する
divA の幅を調べるにはどうすればよいですか? このコードは、空のアラート ポップアップを表示します。
<script type="text/javascript">
function DisplayResult(){
width = document.getElementById("divA").style.width;
alert(width);
}
var widthFinder = {
find: DisplayResult
};
widthFinder.find();
</script>
<div id="divA">Alert this string's length in pixels</div>
編集:
これにより、「null」がアラートされるようになりました。まだ私が望んでいるものではありません。
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
function DisplayResult(){
var width = $("#divA").width();
alert(width);
}
var widthFinder = {
find: DisplayResult
};
widthFinder.find();
</script>
<div id="divA" style="display:inline; width:100%;">Alert this string's length in pixels</div>
</body>
</html>