简体   繁体   中英

How to find the actual width of element in javascript

Hi i am trying to find the actual width of given element and pop the alert which shows the actual width.

code i am using is here..

html element

<table cellspacing="1" cellpadding="5" style=";width:975px;border: 1px solid black;" id="menu_bar">

i am using this:

var widthofmenubar = $(this).find("#menu_bar").width;

Try like

var widthofmenubar = $("#menu_bar").width();

and edit your html like

<table cellspacing="1" cellpadding="5" style="width:975px;border: 1px solid black;" id="menu_bar">

添加() ,您需要执行.width()方法。

var widthofmenubar = $(this).find("#menu_bar").width();
var widthofmenubar = $("#menu_bar").width();
Try this:


First you edit your code, remove ';' after style attr

Correct Code is:

<table cellspacing="1" cellpadding="5" style="width:975px;border: 1px solid black;" id="menu_bar">

Not

<table cellspacing="1" cellpadding="5" style=";width:975px;border: 1px solid black;" id="menu_bar">

// Get Table Width by ID

var tableWidth = $('#menu_bar').css('width');

alert(tableWidth ); // will alert table width

$(this).find("#menu_bar").width(); for actual width that is displayed (rendered) and $(this).find("#menu_bar").css('width') for applied width

there is two methods in jquery for width; 1) .innerWidth 2) .outerwidth

innerwidth calculate without margin width and outerWidth calculate with margin width.

$('#menu_bar').innerWidth();
$('#menu_bar').outerWidth();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM