关于Select宽度设定
在使用Select时,有时候需要固定宽度,有时候需要自适应,下面说下要设定固定宽度时要注意的地方:
控件设定宽度一般是直接指定width属性即可,但Select设定固度是使用style=”width:n”的形式设定,如果只设置width属性,则Select的宽度还是会随下面选择项的宽度变化而变化的,如:
<select width=”20px”>
<option>1111111111111111111</option>
<option>22</option>
<option>1</option>
<option>44444444444444444444444444444444</option>
</select>
则Select的宽度是第四个选项的宽度,即最终页面上显示的Select宽度肯定是大于20px的,如果是这种方式:
<select style=”width:20px”>
<option>1111111111111111111</option>
<option>22</option>
<option>1</option>
<option>44444444444444444444444444444444</option>
</select>
则Select的宽度会固定为20px,即第四个选项会显示不完整,达到了固定Select宽度的目的.
自动调整为最大长度:
<select style=”width:expression_r((this.offsetWidth>100)?’auto’:’100’)”>
<option >hellohellohellohellohellohellohellohellohellohello</option>
</select>
<br/>
鼠标点时自动变成最大长度,离开或选中时恢复原长度
<select name=s2 style=”width:50px” onFocus=”this.style.width=’auto’” οnblur=”this.style.width=’50px’” onChange=”this.style.width = ‘50px’” style=”width:50px”>
<option></option><option>1111111111111111111111111111</option>
<option>2222222222222222222</option>
</select>
<br>
鼠标滑过时自动变成最大长度,离开或选中时恢复原长度
<select name=s2 style=”width:50px” οnmοuseοver=”this.style.width=’auto’” οnblur=”this.style.width=’50px’” onChange=”this.style.width = ‘50px’” style=”width:50px”>
<option></option><option>1111111111111111111111111111</option>
<option>2222222222222222222</option>
</select>
<br>
选择后在页面的其它位置显示选择的项目
<div id=dsp></div>
<select name=”select” size=”1” οnchange=”dsp.innerText=this.options[this.selectedIndex].text”>
<option value=”1”>test</option>
<option value=”2” selected>test2</option>
</select>
<br>
鼠标滑过时显示当前选择的项目的值
<div id=div1 style=”overflow:auto;height:expression_r(5。15);width:expression_r(document.all.s1.offsetWidth+18);”>
<select name=s1 size=1 style=”width:120” οnmοusemοve=”showTitle(this);” οnmοuseοut=”hideTitle(this);”>
<option>aaaaaaaaaaaaaaaaaaaaaa1 </option>
<option >bbbbbbbbbbbbbbbbbbbbbbbbb1 </option>
<option>cccccccccccccccccccccccc1 </option>
<option>ddddddddddddddddddddddd1 </option>
<option>eeeeeeeeeeeeeeeeeeeeeee1 </option>
<option>fffffffffffffffffffffffffff1 </option>
</select>
</div>
<script>
// 定义Popup
var oP=window.createPopup();
oP.document.body.style.background=”beige”;
//popup 是否已显示
var oPShow=0;
function showTitle(obj){
var h=event.offsetY;
var fontsize=(obj.style.fontSize==”“?(obj.currentStyle.fontSize==”“?”9”:obj.currentStyle.fontSize):obj.style.fontSize);
fontsize=parseInt(fontsize)*1.6;
var l=parseInt(h/fontsize);
oP.document.body.innerText=obj.options[l].text;
len=obj.options[l].text.length*8;
oP.show(event.offsetX+20,event.offsetY+fontsize+5,len,fontsize,document.body);
oPShow=1;
}
function hideTitle(obj){
if(oPShow==1)oP.hide();
}
</script>
在使用Select时,有时候需要固定宽度,有时候需要自适应,下面说下要设定固定宽度时要注意的地方:
控件设定宽度一般是直接指定width属性即可,但Select设定固度是使用style=”width:n”的形式设定,如果只设置width属性,则Select的宽度还是会随下面选择项的宽度变化而变化的,如:
<select width=”20px”>
<option>1111111111111111111</option>
<option>22</option>
<option>1</option>
<option>44444444444444444444444444444444</option>
</select>
则Select的宽度是第四个选项的宽度,即最终页面上显示的Select宽度肯定是大于20px的,如果是这种方式:
<select style=”width:20px”>
<option>1111111111111111111</option>
<option>22</option>
<option>1</option>
<option>44444444444444444444444444444444</option>
</select>
则Select的宽度会固定为20px,即第四个选项会显示不完整,达到了固定Select宽度的目的.
自动调整为最大长度:
<select style=”width:expression_r((this.offsetWidth>100)?’auto’:’100’)”>
<option >hellohellohellohellohellohellohellohellohellohello</option>
</select>
<br/>
鼠标点时自动变成最大长度,离开或选中时恢复原长度
<select name=s2 style=”width:50px” onFocus=”this.style.width=’auto’” οnblur=”this.style.width=’50px’” onChange=”this.style.width = ‘50px’” style=”width:50px”>
<option></option><option>1111111111111111111111111111</option>
<option>2222222222222222222</option>
</select>
<br>
鼠标滑过时自动变成最大长度,离开或选中时恢复原长度
<select name=s2 style=”width:50px” οnmοuseοver=”this.style.width=’auto’” οnblur=”this.style.width=’50px’” onChange=”this.style.width = ‘50px’” style=”width:50px”>
<option></option><option>1111111111111111111111111111</option>
<option>2222222222222222222</option>
</select>
<br>
选择后在页面的其它位置显示选择的项目
<div id=dsp></div>
<select name=”select” size=”1” οnchange=”dsp.innerText=this.options[this.selectedIndex].text”>
<option value=”1”>test</option>
<option value=”2” selected>test2</option>
</select>
<br>
鼠标滑过时显示当前选择的项目的值
<div id=div1 style=”overflow:auto;height:expression_r(5。15);width:expression_r(document.all.s1.offsetWidth+18);”>
<select name=s1 size=1 style=”width:120” οnmοusemοve=”showTitle(this);” οnmοuseοut=”hideTitle(this);”>
<option>aaaaaaaaaaaaaaaaaaaaaa1 </option>
<option >bbbbbbbbbbbbbbbbbbbbbbbbb1 </option>
<option>cccccccccccccccccccccccc1 </option>
<option>ddddddddddddddddddddddd1 </option>
<option>eeeeeeeeeeeeeeeeeeeeeee1 </option>
<option>fffffffffffffffffffffffffff1 </option>
</select>
</div>
<script>
// 定义Popup
var oP=window.createPopup();
oP.document.body.style.background=”beige”;
//popup 是否已显示
var oPShow=0;
function showTitle(obj){
var h=event.offsetY;
var fontsize=(obj.style.fontSize==”“?(obj.currentStyle.fontSize==”“?”9”:obj.currentStyle.fontSize):obj.style.fontSize);
fontsize=parseInt(fontsize)*1.6;
var l=parseInt(h/fontsize);
oP.document.body.innerText=obj.options[l].text;
len=obj.options[l].text.length*8;
oP.show(event.offsetX+20,event.offsetY+fontsize+5,len,fontsize,document.body);
oPShow=1;
}
function hideTitle(obj){
if(oPShow==1)oP.hide();
}
</script>