当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 可以编辑的Select (第二版)

Javascript
web开发设计师比较费解的JavaScript
jQuery教程:整理的几个常见的初学者问题
免费资源:7个效果非常棒的jQuery 3D效果插件
JavaScript教程:编写匿名函数的几种方法
jQuery教程:jQuery的核心
jQuery教程:jQuery核心方法的使用
webjx收集45个jQuery导航插件和教程
30个气泡悬浮框(Tooltip)的jQuery插件
Jetpack扩展案例:Gmail邮件提醒功能
非常出色的jQuery运动特效可以和Flash媲美
ImagesLazyLoad 图片延迟加载效果
收集国外的14个图片放大编辑的jQuery插件
修改和创建DOM节点两种方式的4种优化方案
jQuery.Switchable整合插件用途介绍
提高Textarea操作性能优秀的jQuery插件
WEBJX收集12个非常有创意的JavaScript小游戏
Javascript教程:关于深入了解JS的几个问题

Javascript 中的 可以编辑的Select (第二版)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-10   浏览: 204 ::
收藏到网摘: n/a

观看效果

<script src="editableselect.js"></script>
<select id="EditableSelect">
<option>可以编辑的select第二版</option>
<option>Bencalie制作</option>
</select>

=======================
editableselect.js

window.onload=function(){
 var objSelect=EditableSelect
 var obj=document.all.EditableSelect
 getTop=obj.offsetTop;
 getLeft=obj.offsetLeft;
 while(objSelect=objSelect.offsetParent){
 getTop+=objSelect.offsetTop;
 getLeft+=objSelect.offsetLeft;
 }

 var oNewItem=document.createElement("OBJECT"); 
 document.body.insertBefore(oNewItem);
 oNewItem.outerHTML="<object id=editable style=\"z-index:2;position:absolute\" type=\"text/x-scriptlet\" data=\"addin2.htm\"></object>";

 editable.style.left=getLeft+1
 editable.style.top=getTop+1
 editable.style.width=obj.offsetWidth-19
 editable.style.height=obj.offsetHeight-3
 
 obj.onchange=function(){editable.str(obj.options[obj.selectedIndex].text)}
 obj.onresize=function(){editable.style.width=obj.offsetWidth-19}
}

function addNewOption(value){
 EditableSelect.options[EditableSelect.length]=new Option(value,value)
}

=======================
addin2.htm

<script language="vbs">
function public_str(theStrIn)
 strIn.value=theStrIn
end function
</script>
<body leftmargin=0 topmargin=0>
<script language="javascript">
function check(){
var obj=parent.document.all.EditableSelect
var theValue=document.all.strIn.value.replace(/^\s*/g,"").replace(/\s*$/g,"")
if(event.keyCode==13){
if(theValue!=""){
for(i=0;i<obj.length;i++)
 if(obj.options[i].text==theValue){
  alert("该选项已经存在!");
  document.all.strIn.focus();
  document.all.strIn.value="";
  return;
 }
parent.addNewOption(theValue) 
}
document.all.strIn.value=""
}
}
</script>
<input id=strIn style='border:0;width:100%;height:100%;padding-top:2px' onkeydown=check()>
</body>