日期:2014-05-16  浏览次数:20521 次

如何得到span标签的尺寸?
如何得到span标签的尺寸?
例如: <span> abc </span>
怎么在javascript里得到这个span标签在浏览器里显示时的尺寸?


------解决方案--------------------
<span id=a> abc </span>
<input type=button value=get onclick= "alert(document.getElementById( 'a ').offsetWidth);alert(document.getElementById( 'a ').offsetHeight) "/>
------解决方案--------------------
<span id= "span1 "> abc </span>

document.getElementById( "span1 ").offsetWidth
document.getElementById( "span1 ").offsetHeigth
------解决方案--------------------
<script type= "text/javascript ">
function spansize(){
var sp=document.getElementsByTagName( "span ")[0];
var xx=sp.offsetWidth;
var yy=sp.offsetHeight;
alert( "宽= "+xx+ "\n高= "+yy);
}
window.onload=spansize;
</script>
<span> aasdasfbcasdfasfasf </span>