`
fenhonghai
  • 浏览: 15180 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
社区版块
存档分类
最新评论

css+js模拟可变长度下拉选择框/下拉选项长度过长时可换行显示

    博客分类:
  • js
阅读更多
准备一张图片名称为1.gif
与此html放在同一路径下,打开此HTML即可看见效果

此代码也适用于动态加载下拉列表数据,具体方法可具体分析,也欢迎大家来与我讨论。
php中已试用过。


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<style>


.div_1{
height:100;
overflow-x:hidden;
overflow-y:scroll;
cellspacing:0px;
cellpadding:0px;
}

.table_1{
top:0px;
width:100%;
heigth:20;
border:1;
font-size:14px;
cellspacing:0px;
cellpadding:0px;
}
.tr_1{
heigth:20;
width:100%;
cursor:default;
}

</style>

<script>
function moveon(obj){
obj.background = "i.gif";
obj.style.color = "#FFFFFF";
}

function out(obj){
obj.background = "";
obj.style.color = "";
}

function selected(textobj,obj,divobj){
document.getElementById(textobj).value=obj.firstChild.nodeValue;
document.getElementById(divobj).style.display="none";
}


function showdiv(obj){
document.getElementById(obj).style.display="";
}


function showdiv_here(listid,obj,num){

if(num>15){
document.getElementById(listid).style.overflowY="scroll";
document.getElementById(listid).style.height = num*15;
}else{
document.getElementById(listid).style.overflowY="hidden";
document.getElementById(listid).style.height = num*24;
}
document.getElementById(obj).style.display="";

}

function leave(divobj){
document.getElementById(divobj).style.display="none";
}
</script>

</head>

<body cellspacing=0 cellpadding=0> 


<table border=1>
<tr>
<td>列表中的项目小于15的时候,不显示滚动条,列表框高度自适应</td>
<td>
<input type="text" id="name1" value="" onclick="showdiv_here('listdiv','showlist1',4);"  enable="false" onblur="leave('showlist1');"></input>
<div id="A" style="position:relative">

<div id="showlist1" style="display:none;position:absolute;left:0px;top:0px" >
<table  border=0 cellspacing=0 cellpadding=1>
   <tr>
     <td bgcolor="#000000">
<table  bgcolor="#FFFFFF" border=0 cellspacing=0 cellpadding=0>
   <tr>
     <td>
<div id="listdiv" class="div_1">
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name1',this,'showlist1')">123456789123456789123456789123456789123456789123456789</td></tr></table>
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name1',this,'showlist1')">1ssssssssssssssssssssssssssssssssssssssssssssss</td></tr></table>
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name1',this,'showlist1')">2</td></tr></table>
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name1',this,'showlist1')">3</td></tr></table>
</div>
     </td>
   </tr>
</table>
     </td>
   </tr>
</table>
</div >
</div>
</td>
</tr>

<tr>
<td>超过15项之后自滚动条显示</td>
<td>
<input type="text" id="name3" value="" onclick="showdiv_here('listdiv3','showlist3',17);"  enable="false" onblur="leave('showlist3');"></input>
<div id="A" style="position:relative">

<div id="showlist3" style="display:none;position:absolute;left:0px;top:0px" >
<table  border=0 cellspacing=0 cellpadding=1>
   <tr>
     <td bgcolor="#000000">
<table  bgcolor="#FFFFFF" border=0 cellspacing=0 cellpadding=0>
   <tr>
     <td>
<div id="listdiv3" class="div_1">
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name3',this,'showlist3')">1</td></tr></table>
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name3',this,'showlist3')">22222222222222222111111111111</td></tr></table>
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name3',this,'showlist3')">2</td></tr></table>
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name3',this,'showlist3')">3</td></tr></table>
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name3',this,'showlist3')">1</td></tr></table>
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name3',this,'showlist3')"></td></tr></table>
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name3',this,'showlist3')">2</td></tr></table>
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name3',this,'showlist3')">3</td></tr></table>
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name3',this,'showlist3')">1</td></tr></table>
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name3',this,'showlist3')"></td></tr></table>
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name3',this,'showlist3')">2</td></tr></table>
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name3',this,'showlist3')">3</td></tr></table>
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name3',this,'showlist3')">1</td></tr></table>
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name3',this,'showlist3')"></td></tr></table>
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name3',this,'showlist3')">2</td></tr></table>
<table class="table_1"><tr class="tr_1"><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name3',this,'showlist3')">3</td></tr></table>
</div>
     </td>
   </tr>
</table>
     </td>
   </tr>
</table>
</div >
</div>
</td>
</tr>

<tr>
<td>项目文本过长时,分行显示</td>

<td>
<input type="text" id="name" value="" onclick="showdiv('showlist');"  enable="false" onblur="leave('showlist');"></input>
<div id="A" style="position:relative">

<div id="showlist" style="display:none;width:300px;position:absolute;left:0px;top:0px" >
<table style="width:100%;" border=0 cellspacing=0 cellpadding=1>
   <tr>
     <td bgcolor="#000000">
<table style="width:100%" bgcolor="#FFFFFF" border=0 cellspacing=0 cellpadding=0>
   <tr>
     <td>
<div class="div_1">
<table class="table_1" style="word-break:break-all;"><tr><td  onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name',this,'showlist')">123456789123456789123456789123456789123456789123456789</td></tr></table>
<table class="table_1"><tr><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name',this,'showlist')">1ssssssssssssssssssssssssssssssssssssssssssssss</td></tr></table>
<table class="table_1"><tr><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name',this,'showlist')">2</td></tr></table>
<table class="table_1"><tr><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name',this,'showlist')">3</td></tr></table>
<table class="table_1"><tr><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name',this,'showlist')">4</td></tr></table>
<table class="table_1"><tr><td onmouseover='moveon(this)' onmouseout='out(this)'
             onMouseDown ="selected('name',this,'showlist')">5</td></tr></table>
</div>
     </td>
   </tr>
</table>
     </td>
   </tr>
</table>
</div >
</div>
</td>
</tr>
</table>

</body>
</html>



下拉按钮和具体的下拉列表的事件处理,大家完善一下就可以用了。
分享到:
评论

相关推荐

    JQuery&CSS;&CSS;+DIV实例大全.rar

    3.JS+CSS美化经典Select选项框插件 4.JS+CSS通用一个页面同时三个焦点图轮换效果完整实例 5.JS+CSS网页版模拟QQ登录框界面特效示例 6.JS+flash立柱广告代码仿百度MP3搜索右侧可伸缩的立柱广告效果 7.JS版本...

    程序天下:JavaScript实例自学手册

    2.13 选择不同的列表项就显示不同的按钮 2.14 使用按钮控制文本渐变 2.15 翻页效果的公告栏 2.16 动态设置控件的事件 第3章 字符串文本和输入框特效 3.1 只带下划线的输入框 3.2 限定文本框可输入字符数 3.3 文字过...

    《程序天下:JavaScript实例自学手册》光盘源码

    2.13 选择不同的列表项就显示不同的按钮 2.14 使用按钮控制文本渐变 2.15 翻页效果的公告栏 2.16 动态设置控件的事件 第3章 字符串文本和输入框特效 3.1 只带下划线的输入框 3.2 限定文本框可输入字符数 3.3 文字过...

    ExtAspNet v2.2.1 (2009-4-1) 值得一看

    -重新设计模拟树的下拉列表的实现,避免选中某项后的闪烁。 +2009-11-21 v2.1.5 +Tree优化。 -修正Expanded项和Checked项的状态在回发改变后不能保持的BUG。 -GetNodeById更名为FindNode,保持和...

    ExtAspNet_v2.3.2_dll

    -重新设计模拟树的下拉列表的实现,避免选中某项后的闪烁。 +2009-11-21 v2.1.5 +Tree优化。 -修正Expanded项和Checked项的状态在回发改变后不能保持的BUG。 -GetNodeById更名为FindNode,保持和...

    JavaScript王者归来part.1 总数2

     12.8.2 如何使用XML DOM--一个利用XML实现多级关联下拉选择框的例子   12.9 总结   第13章 事件处理  13.1 什么是事件   13.1.1 消息与事件响应   13.1.2 浏览器的事件驱动机制   13.2 基本事件处理  ...

    CSS-Tools:MDN CSS生成工具

    纯JavaScript可在Firefox,Chrome,Safary,Edge,IE9 +上运行 CSS工具 盒子阴影 拖放框阴影生成器 无限数量的阴影 使用:before和:after pseude-class支持2个附加层 边界半径 边框图像 进阶ColorPicker 创建自定义...

    文章管理系统

    2.列表页导航分页加入超过100分页时,下拉选择自动变成文本框输入,防止下拉项太多而影响速度 3.纠正文章管理因异常栏目信息,而造成页面错误 4.生成的缓存文件加入和%&gt;字符的编码,防止因配置内容含asp代码而造成...

    Ext Js权威指南(.zip.001

    Ex4.0共2个压缩包特性,《ext js权威指南》 前 言 第1章 ext js 4开发入门 / 1 1.1 学习ext js必需的基础知识 / 1 1.2 json概述 / 3 1.2.1 认识json / 3 1.2.2 json的结构 / 3 1.2.3 json的例子 / 4 1.2.4 ...

    dreamweaver网站PHP开发MXP插件

    很COOL的可变大小的层插件.mxp 横向数据重复的插件.mxp 换行显示搜索高亮度文本插件.mxp 记录集跳转菜单插件.mxp 检查新元素插件.mxp 检验控制组件插件.mxp 简约版高级分页插件.mxp 截取字符长度.mxp 酷酷的表格边框...

    400个DreamWeaver插件

    mxp/在代码编辑框中选择一段脚本代码,然后使用这个插件,可以把这些代码放到一个单独的js文件中 mxp/在Dreamweaver中快速的插入一个Fireworks做好的图片,不过好象只能做空白图 :( mxp/这个插件用来代替...

    study:学习过程分享

    模拟下拉选择框,慕客网素材改进版本 react_webpack 手工配置 react + webpack 的例子 支持HMR,ES7语法,css前缀补全 2048 2048网页游戏js版 通过观察原版2048,分析游戏的逻辑,用js+css+html编程 hybird 页面动态...

    精通javascript

    2.选择浏览器的“工具”-“安全”-“本地Intranet”-“自定义级别”-“活动脚本”中选用“禁用”单选框 3.在浏览器中输入下述地址:http://localhost/1.2.htm • 1.1.htm 多段代码相互调用 • 1.2....

    精通JavaScript

    • 5.7.htm 参数变量可变的函数 • 5.8.htm 动态函数 • 5.9.htm encodeURI方法 • 5.10.htm decodeURI方法 • 5.11.htm decodeURI方法 • 5.12.htm decodeURI方法...

    PHP开发实战1200例源码

    实例178 去掉下拉选项的边框 215 实例179 修改表单属性为弹出窗口 216 实例180 表单输入单元的文字设置 217 实例181 表单输入单元单击删除 218 实例182 表单文本输入的移动选择 219 实例183 通过下拉列表选择头像 ...

    IBM WebSphere Portal门户开发笔记01

    13、CSS水平下拉菜单被FLASH覆盖如何解决 253 14、禁止选择页面上的文字来拷贝 254 15、JS屏蔽右键菜单 255 16、禁止保存网页 255 17、CSS和JS文件引入方式 255 18、弹出窗口居中显示JS代码 255 19、CSS渐变样式 256...

    CodeIgniter:php敏捷开发框架web快速开发详解

    实现发送 Email 的功能中有一些不容易解决的技术问题:比如设置文本自动换行(取消设置的话则可以保持长 URL 地址不被换行或截断)或发送附件。标准的 PHP 实现起来比较复杂,CI 简化了这些工作,它的 Email 类使得...

    PHP开发实战1200例(第1卷).(清华出版.潘凯华.刘中华).part1

    实例178 去掉下拉选项的边框 215 实例179 修改表单属性为弹出窗口 216 实例180 表单输入单元的文字设置 217 实例181 表单输入单元单击删除 218 实例182 表单文本输入的移动选择 219 实例183 通过下拉列表选择头像 ...

    PHP开发实战1200例(第1卷).(清华出版.潘凯华.刘中华).part2

    实例178 去掉下拉选项的边框 215 实例179 修改表单属性为弹出窗口 216 实例180 表单输入单元的文字设置 217 实例181 表单输入单元单击删除 218 实例182 表单文本输入的移动选择 219 实例183 通过下拉列表选择头像 ...

Global site tag (gtag.js) - Google Analytics