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

一个布局css怎么来实现?
本帖最后由 yunfeifan 于 2014-03-18 17:56:34 编辑
一个布局如此:



一个div(div1) 包含一个div(div11)和input(input11)
div1的宽度不定,div11的宽度不定,怎么能让
1. div11的宽度不是死的,我如果给的文字多自动撑满。
2. input11的宽度正好撑满div1剩余的空间?
------解决方案--------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
*{
margin:0;
padding:0;
}
body{
font-size:12px;
}

</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
</head>
<body>
<div class="div1" style="float:left;border:1px solid red;width:auto;">
 <div class="div11" style="float:left;border:1px solid green;width:auto;">
 ddddddddddddddfasssss
 </div>
 <input type="text" class="input11" style="float:left;border:1px solid blue;" />
</div>
</body>
</html>


------解决方案--------------------
如果想实现,要结合JS来计算布局后的宽度,你可以尝试如下的代码,随意改变box的宽度


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
.box{width:500px;margin:100px auto;height:22px;line-height:22px;}
.box span{float:left;}
.box input{float:left;border:solid 1px #ccc; margin:0;height:22px;}
</style>
<script>
window.onload=function()
{
var oBox=document.getElementById('box');
var oSpan=oBox.getElementsByTagName('span')[0];
var oText=oBox.getElementsByTagName('input')[0];
oText.style.width=getStyle(oBox,"width")-getStyle(oSpan,"width")-2*getStyle(oText,"borderLeftWidth")+'px';
}
function getStyle(obj,attr)
{
if(obj.currentStyle)
return parseFloat(obj.currentStyle[attr])
------解决方案--------------------
0;
return parseFloat(getComputedStyle(obj,0)[attr])
------解决方案--------------------
0;
}
</script>
</head>
<body>
<div class="box" id="box">
<span>测试测试测试测绘试测绘试测绘</span>
    <input type="text" />
</div>
</body>
</html>


------解决方案--------------------
引用:
如果想实现,要结合JS来计算布局后的宽度,你可以尝试如下的代码,随意改变box的宽度


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;