irefox和ie下的select option js相关操作

FireFox中,很多的js操作与IE中是不同的。现在本文就不同操作给予说明。

JS 操作select 标签

1, 添加 option

< select>
< option value="aaa">123</ option>
< option value="bbb">456</ option>
</ select>
<button οnclick="myOption=document.getElementsByTagName(' select')[0];myOption.options[myOption.options.length]=new Option('guoshuangText','guoshuangValue')"> add option</button>





2, 删除 optionfirefoxselect.remove(selectedIndex),而不是 select.options.remove()

针对这一问题在编写代码中有一个解决办法:

try{

select1.options.remove(j);

// 首先执行这个操作,因为大部分用户都是使用ie

}

catch(e)

{

// 如果抛出异常的话,就尝试firefox的操作

select1.remove(j);

}


3,输出 option[x].innerText 在 firefox 下用 options[x].textContent(firefox没有innerText,就是用textContent 来替代的)

< select οnchange=”alert(this[selectedIndex].textContent)”>
< option value=”aaa”>123</ option>
< option value=”bbb”>456</ option>
</ select>


4,在firefox 中判断select是否选中,不能用

for(var j=0 ; j < select1.options.length;j ++)

{

if(select1.options[j].selected){}

}


而是应该这样

for(var j=0 ; j < select1.options.length;j ++) {

var checka = select1.options[j].selected;

if(checka){} }


这种情况在ie和firefox中是通用的,但是为什么这样呢,具体原因现在还不清楚。

5,对于select 的onclick事件,ie支持在select中设置onclick事件,在option中设置无效,但是在Firefox中,支持option的 onclick事件,在select中设置无效。另外,如果在onclick中想要得到点击的是哪一个option,是得不到的。为什么呢?因为在点击事件是发生在选择之前的,只有点击之后才能确定哪一个被选中了。所以在这是设置了一个定时函数,在单击之后60ms执行来判断哪个选中。

下面给出了解决办法。

<script type="text/javascript" >



function simOptionClick4IE(){

var evt=window.event ;

var selectObj=evt?evt.srcElement:null;

// IE Only

if (evt && selectObj ) ) { // 事件存在



// 记录原先的选中项

var oldIdx = selectObj.selectedIndex;



setTimeout(function(){

var option=selectObj.options[selectObj.selectedIndex];

// 此时可以通过判断 oldIdx 是否等于 selectObj.selectedIndex

// 来判断用户是不是点击了同一个选项,进而做不同的处理.

showOptionValue( option)



}, 60);

}

}

function showOptionValue(opt,msg){

var now=new Date();

var dt= (1900+now.getYear())+'-'+(now.getMonth()+1)+'-'+now.getDate()+

' '+now.getHours()+':'+now.getHours()+':'+now.getSeconds()+'.'+now.getMilliseconds();

var resultZone=document.getElementById('reslut');

resultZone.style.margin="10px";

resultZone.innerHTML=dt +" 时,点击了: " + (opt.text + ' = '+opt.value);

}

</script>

< select οnclick="simOptionClick4IE()" >

<!-- 下面的 οnclick="showOptionValue( this )" 是为 ff 和 opera而准备 -->

< option value="1" οnclick="showOptionValue( this )" >aaaaa</ option>

< option value="2" οnclick="showOptionValue( this )" >bbbbb</ option>

< option value="3" οnclick="showOptionValue( this )" >ccccc</ option>

</ select> <div id="reslut" ></div>

IE 6中使用document.createElement和appendChild无法显示

<script type=”text/javascript”>
var i = 0;
function change(){
i++;
var tb=document.getElementById(”t1″)
var txt = document.createElement(”input”);
var tr1 = document.createElement(”tr”);
var td1= document.createElement(”td”);
var td2= document.createElement(”td”);
td2.setAttribute(”width”,”123″)
td2.setAttribute(”bordercolor”,”#FFFFFF”)
td2.setAttribute(”bgcolor”,”#CAE2EB”)

td1.setAttribute(”width”,”401″)
td1.setAttribute(”align”,”left”)
td1.setAttribute(”bordercolor”,”#FFFFFF”)
td1.setAttribute(”bgcolor”,”#FFFFFF”)
td1.innerHTML=”phone”+i+”1″;
td2.innerHTML=”电话号码”+i+”1″;
txt.setAttribute(”name”,”phone”+i+”1″);
txt.setAttribute(”type”,”text”);
txt.setAttribute(”size”,”15″);
txt.setAttribute(”maxlength”,”15″);

td2.appendChild(td2);
tr1.appendChild(td2);
tr1.appendChild(td1);

td1.appendChild(txt);
‘tb.appendChild(tr1);
tb.appendChild(tr1);
}
</script>

问题IE6对appendChild的函数不支持,

IE6 增加TR用 TABLEOBJ.insertRow()
增加TD用 TROBJ.insertCell()

参考IE6可正常执行的代码:

<script>
var j=0
function add_row(){
k=j+1
j=sqd.rows.length;
newRow=document.all.sqd.insertRow(-1)
newcell=newRow.insertCell()
newcell.style.backgroundColor=’#f7f7f7′
newcell.innerHTML=j
newcell=newRow.insertCell()
newcell.style.backgroundColor=’#f7f7f7′
newcell.innerHTML=”<input name=Number type=radio >”
newcell=newRow.insertCell()
newcell.style.backgroundColor=’#f7f7f7′
newcell.innerHTML=”<input name=SerialNumber type=text class=INPUT1 size=3>”
newcell=newRow.insertCell()
newcell.style.backgroundColor=’#f7f7f7′
newcell.innerHTML=”<input name=ProductName type=text class=INPUT1 size=3>”
newcell=newRow.insertCell()
newcell.style.backgroundColor=’#f7f7f7′
newcell.innerHTML=”<input name=PieceNo type=text class=INPUT1 size=3>”
newcell=newRow.insertCell()
newcell.style.backgroundColor=’#f7f7f7′
newcell.innerHTML=”<input name=Quantity type=text class=INPUT1 size=3>”
newcell=newRow.insertCell()
newcell.style.backgroundColor=’#f7f7f7′
newcell.innerHTML=”<input name=GrossWeight type=text class=INPUT1 size=3>”
newcell=newRow.insertCell()
newcell.style.backgroundColor=’#f7f7f7′
newcell.innerHTML=”<input name=CountWeight type=text class=INPUT1 size=3>”
newcell=newRow.insertCell()
newcell.style.backgroundColor=’#f7f7f7′
newcell.innerHTML=”<input name=UnitPrice type=text class=INPUT1 size=3>”
newcell=newRow.insertCell()
newcell.style.backgroundColor=’#f7f7f7′
newcell.innerHTML=”<input name=Amount type=text class=INPUT1 size=3>”
newcell=newRow.insertCell()
newcell.style.backgroundColor=’#f7f7f7′
newcell.innerHTML=”<input name=CustOrder type=text class=INPUT1 size=3>”
newcell=newRow.insertCell()
newcell.style.backgroundColor=’#f7f7f7′
newcell.innerHTML=”<input name=Description type=text class=INPUT1 size=3>”
}
function del_row() {
if(sqd.rows.length==1) return;
var checkit = false
for (var i=0;i<document.all.Number.length;i++) {
if (document.all.Number[i].checked) {
checkit=true;
sqd.deleteRow(i)
break;
}
}
if (checkit) {
for(i=1;i<sqd.rows.length;i++){
sqd.rows[i].cells[0].innerText=i
}

} else
{
alert(”请选择一个要删除的对象”);
return false}
}
</script><table width=90% border=0 align=”center” cellpadding=2 cellspacing=1 bgcolor=”#FFFFFF”>
<tr>
<td height=”25″ class=”table1″>
<div align=”center”><font color=”#FFFFFF”><strong>≡≡≡ 货 运 单 概 要 ≡≡≡</strong></font></div></td>
</tr>
<tr>
<td align=center valign=top bgcolor=f7f7f7><table id=”sqd” width=”90%” border=”0″ cellpadding=”0″ cellspacing=”1″ bgcolor=”#999999″>
<tr bgcolor=”#BAC2DA”>
<td height=”25″>
<div align=”center”>序号</div></td>
<td>
<div align=”center”>选中
<input name=Number type=hidden >
</div></td>
<td>
<div align=”center”>货物编号</div></td>
<td>
<div align=”center”>货物品名</div></td>
<td>
<div align=”center”>件数</div></td>
<td>
<div align=”center”>数量</div></td>
<td>
<div align=”center”>毛重</div></td>
<td>
<div align=”center”>计货重量</div></td>
<td>
<div align=”center”>费率</div></td>
<td>
<div align=”center”>金额</div></td>
<td>
<div align=”center”>客户定单号</div></td>
<td>
<div align=”center”>备注</div></td>
</tr>
</table>
<br>
<input name=”Submit5″ type=”button” onClick=add_row() class=”button” value=” 添 加 “>
<input name=”Submit33″ οnclick=del_row() type=”button” class=”button” value=” 删 除 “>
</td>
</tr>
</table>




本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2010/01/05/1639907.html,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值