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

该下拉框的长度如何调节?
我的下拉框由style sheet (xsl文件)生成如下:
<select name="SLk1" id="SLk1" size="1"><option value="OBW">That Begins With</option><option value="OOS">That Contains</option><option value="OEM" selected="true">That Equals</option></select>


但是这个页面load时有一种情况是需要将“That Begins With”和“That Contains”给去掉。我用的方法如下:
var sel = document.getElementById('SLk1');
...
sel.removeChild(sel.options[0]); 


这样的话SLk1下拉框的内容长度就比正常请况短了,刚好能容纳“That Equals”。
请问如何使得下拉框的内容长度和原来一样,就是一直是能容纳原来的3个内容,即使出现只有“That Equals”的情况。
谢谢!
------解决方案--------------------
下拉框的宽度是根据你的text宽度来显示的。当你继续增加一个“3个内容”的option 时,它又变长了,因此没必要去控制。
------解决方案--------------------
把样式中的宽度写死不就得了
<!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>
    <script type="text/javascript">
        function test(){
            var sel = document.getElementById('SLk1');
            if(sel.options[0])
                sel.removeChild(sel.options[0]); 
        }
    </script>
</head>
<body>
    <select name="SLk1" id="SLk1" size="1" style="width:200px;" >
        <option value="OBW">That Begins With</option>
        <option value="OOS">That Contains</option>
        <option value="OEM" selected="selected">That Equals</option>
    </select>
    <br />
    <input type="button" onclick="test()" value="测试" />
</body>
</html>

------解决方案--------------------
<select size="5"></select>
------解决方案--------------------
size试试