<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
//添加新项
function Button1_Click()
{
if(!document.getElementById)
{
return true;
}
var newoption=document.createElement("OPTION");
var txtBox1=document.getElementById("TextBox1");
var txtBox2=document.getElementById("TextBox2");
// alert(txtBox1.value);
// alert(txtBox2.value);
newoption.text=txtBox1.value;
newoption.value=txtBox2.value;
var listBox1=document.getElementById("ListBox1");
listBox1.add(newoption);
return false;
}
//得到设置最大数量
//原Capacity的容量为8,默认的情况下会随着新增项(ListItem)的数量增多会成2倍的增长8 16 32..
//但如果设置了Capacity的大小,就不会自动增长了
function Button2_Click()
{
//研究中....
return false;
}
//清空options集合
//html select元素没有一下全部清空optins集合的方法,只能一个一个的清除
function Button3_Click()
{
if(!document.getElementById)
{
return true;
}
var listBox=document.getElementById("ListBox1");
// alert(listBox.options.length);
//
//用这样的方法删除所有的元素不会成功,因为lenth属性会在删除一个元素后发生变第
// for(var i=0;i<=listBox.options.length;i++)
// {
// listBox.remove(i);
// }
while(listBox.options.length>0)
{
listBox.remove(0);
}
return false;
}
//判断某个元素在不在options集合里面
function Button4_Click()
{
if(!document.getElementById)
{
return true;
}
var listBox=document.getElementById("ListBox1");
var txtBox1=document.getElementById("TextBox1");
var txtBox2=document.getElementById("TextBox2");
var ok=false;
for(var i=0;i<listBox.options.length;i++)
{
if((listBox.options[i].text==txtBox1.value) && (listBox.options[i].value==txtBox2.value))
{
ok=true;
break;
}
}
if(ok)
{
alert('yes');
}
else
{
alert('no');
}
return false;
}
//copy to
function Button5_Click()
{
if(!document.getElementById)
{
return true;
}
var itemtexts=new Array();
var itemvalues=new Array();
var listBox=document.getElementById("ListBox1");
for(var i=0;i<listBox.options.length;i++)
{
itemtexts[i]=listBox.options[i].text;
itemvalues[i]=listBox.options[i].value;
}
var listBox2=document.createElement("select");
var newOption=null;
for(var i=0;i<itemtexts.length;i++)
{
newOption=document.createElement("OPTION");
newOption.text=itemtexts[i];
newOption.value=itemvalues[i];
listBox2.add(newOption);
}
var parent=listBox.parentElement;
parent.insertBefore(listBox2,listBox);
return false;
}
//总共几项
function Button6_Click()
{
if(!document.getElementById)
{
return true;
}
var listBox=document.getElementById("ListBox1");
alert(listBox.options.length);
return false;
}
//根据text查找
function Button7_Click()
{
if(!document.getElementById)
{
return true;
}
var listBox=document.getElementById("ListBox1");
var txtBox1=document.getElementById("TextBox1");
var ok=false;
for(var i=0;i<listBox.options.length;i++)
{
if(listBox.options[i].text==txtBox1.value)
{
alert(listBox.options[i].text+":"+listBox.options[i].value);
ok=true;
break;
}
}
if(!ok)
{
alert("没找到");
}
return false;
}
//根据value查找
function Button8_Click()
{
if(!document.getElementById)
{
return true;
}
var listBox=document.getElementById("ListBox1");
var txtBox2=document.getElementById("TextBox2");
var ok=false;
for(var i=0;i<listBox.options.length;i++)
{
if(listBox.options[i].value==txtBox2.value)
{
alert(listBox.options[i].text+":"+listBox.options[i].value);
ok=true;
break;
}
}
if(!ok)
{
alert("没找到");
}
return false;
}
//返回要找值的索引
function Button9_Click()
{
if(!document.getElementById)
{
return true;
}
var listBox=document.getElementById("ListBox1");
var txtBox2=document.getElementById("TextBox2");
var ok=false;
for(var i=0;i<listBox.options.length;i++)
{
if(listBox.options[i].value==txtBox2.value)
{
alert("索引是"+i);
ok=true;
break;
}
}
if(!ok)
{
alert("没找到");
}
return false;
}
//插入元素
function Button10_Click()
{
if(!document.getElementById)
{
return true;
}
var listBox=document.getElementById("ListBox1");
var newOption=document.createElement("option");
newOption.text=document.getElementById("TextBox3").value;
newOption.value=document.getElementById("TextBox4").value;
//填加新元素后再与要插入位置的元素交换
listBox.add(newOption);
var firstchild=listBox.options[0];
var text=firstchild.text;
var value=firstchild.value;
firstchild.text=newOption.text;
firstchild.value=newOption.value;
newOption.text=text;
newOption.value=value;
return false;
}
//删除元素
function Button12_Click(i)
{
if(!document.getElementById)
{
return true;
}
var listBox=document.getElementById("ListBox1");
listBox.remove(listBox.options[i]);
return false;
}
//选择元素
function Button13_Click()
{
if(!document.getElementById)
{
return true;
}
var listBox=document.getElementById("ListBox1");
for(var i=0;i<listBox.options.length;i++)
{
if(listBox.options[i].selected)
{
alert(listBox.options[i].text+":"+listBox.options[i].value);
break;
}
}
return false;
}
//修改元素
function Button14_Click()
{
if(!document.getElementById)
{
return true;
}
var listBox=document.getElementById("ListBox1");
for(var i=0;i<listBox.options.length;i++)
{
if(listBox.options[i].selected)
{
listBox.options[i].text=document.getElementById("TextBox3").value;
listBox.options[i].value=document.getElementById("TextBox4").value;
break;
}
}
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem Value="0">张三</asp:ListItem>
<asp:ListItem Value="1">李四</asp:ListItem>
<asp:ListItem Value="2">王五</asp:ListItem>
<asp:ListItem Value="3">赵六</asp:ListItem>
<asp:ListItem Value="4">王七</asp:ListItem>
</asp:ListBox>
<br />
<br />
oldText
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
oldValue
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<br />
newText
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
newValue<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
<br />
<asp:Button ID="Button1" runat="server" OnClientClick ="return Button1_Click();" Text="add" /><br />
<asp:Button ID="Button2" runat="server" OnClientClick="return Button2_Click();" Text="get(Set)Capacity" /><br />
<asp:Button ID="Button3" runat="server" OnClientClick="return Button3_Click();" Text="Clear" /><br />
<asp:Button ID="Button4" runat="server" OnClientClick="return Button4_Click();" Text="Contains" /><br />
<asp:Button ID="Button5" runat="server" OnClientClick="return Button5_Click();" Text="copyTo" /><br />
<asp:Button ID="Button6" runat="server" OnClientClick="return Button6_Click();" Text="count" />
<br />
<asp:Button ID="Button7" runat="server" OnClientClick="return Button7_Click();" Text="findByText" /><br />
<asp:Button ID="Button8" runat="server" OnClientClick="return Button8_Click();" Text="findByValue" /><br />
<asp:Button ID="Button9" runat="server" OnClientClick="return Button9_Click();" Text="indexOf" />
<br />
<asp:Button ID="Button10" runat="server" OnClientClick="return Button10_Click();" Text="insert" />
<br />
<asp:Button ID="Button11" runat="server" OnClientClick="return Button11_Click();" Text="remove" />
<br />
<asp:Button ID="Button12" runat="server" OnClientClick="return Button12_Click(2);" Text="removeAt" />
<br />
<asp:Button ID="Button13" runat="server" OnClientClick="return Button13_Click();" Text="select" />
<br />
<asp:Button ID="Button14" runat="server" OnClientClick="return Button14_Click();" Text="edit" /><%-- --%></div>
</div>
</form>
</body>
</html>
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wahaccp/archive/2008/07/08/2626285.aspx
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wahaccp/archive/2008/07/08/2626285.aspx