|
阅读:8619回复:3
请教一个老问题,为什么我的代码在IE下可以,在Firefox下不行。
在IE下面,input的值是自动写上的。而在Firefox下,就有个错误,不能显示。代码如下,请高手指点,多谢先。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title></title>
<script type="text/javascript">
function insertHtml(where, el, html){
where = where.toLowerCase();
if(el.insertAdjacentHTML){
switch(where){
case "beforebegin":
el.insertAdjacentHTML('BeforeBegin', html);
return el.previousSibling;
case "afterbegin":
el.insertAdjacentHTML('AfterBegin', html);
return el.firstChild;
case "beforeend":
el.insertAdjacentHTML('BeforeEnd', html);
return el.lastChild;
case "afterend":
el.insertAdjacentHTML('AfterEnd', html);
return el.nextSibling;
}
throw 'Illegal insertion point -> "' + where + '"';
}
var range = el.ownerDocument.createRange();
var frag;
switch(where){
case "beforebegin":
range.setStartBefore(el);
frag = range.createContextualFragment(html);
el.parentNode.insertBefore(frag, el);
return el.previousSibling;
case "afterbegin":
if(el.firstChild){
range.setStartBefore(el.firstChild);
frag = range.createContextualFragment(html);
el.insertBefore(frag, el.firstChild);
return el.firstChild;
}else{
el.innerHTML = html;
return el.firstChild;
}
case "beforeend":
if(el.lastChild){
range.setStartAfter(el.lastChild);
frag = range.createContextualFragment(html);
el.appendChild(frag);
return el.lastChild;
}else{
el.innerHTML = html;
return el.lastChild;
}
case "afterend":
range.setStartAfter(el);
frag = range.createContextualFragment(html);
el.parentNode.insertBefore(frag, el.nextSibling);
return el.nextSibling;
}
throw 'Illegal insertion point -> "' + where + '"';
}
</script>
<script type="text/javascript">
var mydivAddContactButton;
var myHtmlString;
function Main()
{
mydivAddContactButton = document.getElementById("divAddContactButton");
if(2 == 0)
{
// add a single contact to show the user where to start
AddContact();
}
else
{
ShowOldContacts();
}
}
function ShowOldContacts()
{
AddContactWithValues("Account","Tom","1039717","tom.lee@hotmail.com");
AddContactWithValues("CEO","","","moller@yahoo.com");
GetContactsNo();
}
function AddContact()
{
if(myHtmlString!="")
{
myHtmlString = document.getElementById("HiddenContactRow").innerHTML;
}
insertHtml("beforeBegin", mydivAddContactButton, myHtmlString);
//mydivAddContactButton.scrollIntoView(false);
var aContactObjects = document.getElementsByName("ContactObject");
var iContactCount = aContactObjects.length -1;
var aContactIdObjects = document.getElementsByName("ContactId");
var aRoleIdObjects = document.getElementsByName("RoleId");
var aNameIdObjects = document.getElementsByName("NameId");
var aPhoneIdObjects = document.getElementsByName("PhoneId");
var aEmailIdObjects = document.getElementsByName("EmailId");
var iContactIdObjects = aContactIdObjects.length -1;
aContactIdObjects.item(iContactIdObjects).name = "ContactId"+iContactCount;
//aContactIdObjects.item(iContactIdObjects).value = "ContactId"+iContactCount;
//document.getElementById("ContactCount").value = "ContactId"+iContactCount;
aRoleIdObjects.item(iContactIdObjects).name = "RoleId"+iContactCount;
aNameIdObjects.item(iContactIdObjects).name = "NameId"+iContactCount;
aPhoneIdObjects.item(iContactIdObjects).name = "PhoneId"+iContactCount;
aEmailIdObjects.item(iContactIdObjects).name = "EmailId"+iContactCount;
GetContactsNo();
}
function AddContactWithValues(sTitle, sName, sPhone, sEmail)
{
if(myHtmlString!="")
{
myHtmlString = document.getElementById("HiddenContactRow").innerHTML;
}
insertHtml("beforeBegin", mydivAddContactButton, myHtmlString);
//mydivAddContactButton.scrollIntoView(false);
var aContactObjects = document.getElementsByName("ContactObject");
var iContactCount = aContactObjects.length -1;
var aContactIdObjects = document.getElementsByName("ContactId");
var aRoleIdObjects = document.getElementsByName("RoleId");
var aNameIdObjects = document.getElementsByName("NameId");
var aPhoneIdObjects = document.getElementsByName("PhoneId");
var aEmailIdObjects = document.getElementsByName("EmailId");
var iContactIdObjects = aContactIdObjects.length -1;
aContactIdObjects.item(iContactIdObjects).name = "ContactId"+iContactCount;
aRoleIdObjects.item(iContactIdObjects).name = "RoleId"+iContactCount;
aNameIdObjects.item(iContactIdObjects).name = "NameId"+iContactCount;
aPhoneIdObjects.item(iContactIdObjects).name = "PhoneId"+iContactCount;
aEmailIdObjects.item(iContactIdObjects).name = "EmailId"+iContactCount;
aRoleIdObjects.item(iContactIdObjects).value = sTitle;
aNameIdObjects.item(iContactIdObjects).value = sName;
aPhoneIdObjects.item(iContactIdObjects).value = sPhone;
aEmailIdObjects.item(iContactIdObjects).value = sEmail;
}
</script>
</head>
<body onLoad="javascript:Main()">
<form action="abc.php" method="post" onSubmit="javascript:return Submit()">
<span id="HiddenContactRow" style="display: none">
<span class="ContactObject" name="ContactObject" id="ContactObject">
<table border="0" cellpadding="0" cellspacing="0" width="720">
<tr>
<td width="59"><input name="ContactId" id="ContactId" type="text" value="" size="2" /></td>
<td width="102"><input name="RoleId" id="RoleId" type="text" value="" maxlength="20" size="20" /></td>
<td width="113"><input name="NameId" id="NameId" type="text" value="" maxlength="100" size="20" /></td>
<td width="151"><input name="PhoneId" id="PhoneId" type="text" value="" maxlength="20" size="20" /></td>
<td width="232"><input name="EmailId" id="EmailId" type="text" value="" maxlength="100" size="20" /></td>
<td width="37"><img src="images/DelIcon.png" width="16" height="16" onClick="Delete(this)" alt="Delete contact"></td>
</tr>
</table>
</span>
</span>
<table width="720" border="0">
<tr align="left">
<th width="59">Number:</th>
<th width="102">Role:</th>
<th width="113">Name:</th>
<th width="151">Phone Number:</th>
<th width="232">Email:</th>
<th width="37" style="display: none" >Del</th>
</tr>
</table>
<div id="ContactRows">
</div>
<p>
<div align="right" id="divAddContactButton">
<input type="button" id="AddContactButton" value="Add Contact" class="inpAddContact" onClick="AddContact()"/>
</div>
</p>
<p>
<input type="hidden" name="ContactCount" id="ContactCount" value=""/>
<input type="submit" id="SubmitButton" name="SubmitButton" value="Submit"/>
</p>
</form>
</body>
</html> |
|
|
1楼#
发布于:2008-01-17 22:32
问题在下面几句
aContactIdObjects.item(iContactIdObjects).name = "ContactId"+iContactCount; aRoleIdObjects.item(iContactIdObjects).name = "RoleId"+iContactCount; aNameIdObjects.item(iContactIdObjects).name = "NameId"+iContactCount; aPhoneIdObjects.item(iContactIdObjects).name = "PhoneId"+iContactCount; aEmailIdObjects.item(iContactIdObjects).name = "EmailId"+iContactCount; aRoleIdObjects.item(iContactIdObjects).value = sTitle; aNameIdObjects.item(iContactIdObjects).value = sName; aPhoneIdObjects.item(iContactIdObjects).value = sPhone; aEmailIdObjects.item(iContactIdObjects).value = sEmail; 用aRoleIdObjects为例,这个数组是这样获得的 var aRoleIdObjects = document.getElementsByName("RoleId"); 而后修改了aRoleIdObjects.item(iContactIdObjects).name的值后,FF就将这个对象从aRoleIdObjects 中去除了(因为该对对象的name变了,就不属于document.getElementsByName("RoleId")这个集合了),因此 aRoleIdObjects.item(iContactIdObjects)是无法取到任何对象的 (要再次获得这个对象必须用document.getElementsByName("RoleId"+iContactCount);) 而IE并没有这么做 解决方法: 不改变程序原意的话 把function AddContactWithValues(sTitle, sName, sPhone, sEmail)中的 aRoleIdObjects.item(iContactIdObjects).name = "RoleId"+iContactCount; aNameIdObjects.item(iContactIdObjects).name = "NameId"+iContactCount; aPhoneIdObjects.item(iContactIdObjects).name = "PhoneId"+iContactCount; aEmailIdObjects.item(iContactIdObjects).name = "EmailId"+iContactCount; aRoleIdObjects.item(iContactIdObjects).value = sTitle; aNameIdObjects.item(iContactIdObjects).value = sName; aPhoneIdObjects.item(iContactIdObjects).value = sPhone; aEmailIdObjects.item(iContactIdObjects).value = sEmail; 改成 aRoleIdObjects.item(iContactIdObjects).value = sTitle; aNameIdObjects.item(iContactIdObjects).value = sName; aPhoneIdObjects.item(iContactIdObjects).value = sPhone; aEmailIdObjects.item(iContactIdObjects).value = sEmail; aRoleIdObjects.item(iContactIdObjects).name = "RoleId"+iContactCount; aNameIdObjects.item(iContactIdObjects).name = "NameId"+iContactCount; aPhoneIdObjects.item(iContactIdObjects).name = "PhoneId"+iContactCount; aEmailIdObjects.item(iContactIdObjects).name = "EmailId"+iContactCount; 即可(即先修改value再修改name) PS:以我的理解这些input对象的name不应该修改,而应该修改id(同类对象使用相同的name和不同的id) 把 aRoleIdObjects.item(iContactIdObjects).name = "RoleId"+iContactCount; aNameIdObjects.item(iContactIdObjects).name = "NameId"+iContactCount; aPhoneIdObjects.item(iContactIdObjects).name = "PhoneId"+iContactCount; aEmailIdObjects.item(iContactIdObjects).name = "EmailId"+iContactCount; 改成 aRoleIdObjects.item(iContactIdObjects).id= "RoleId"+iContactCount; aNameIdObjects.item(iContactIdObjects).id= "NameId"+iContactCount; aPhoneIdObjects.item(iContactIdObjects).id= "PhoneId"+iContactCount; aEmailIdObjects.item(iContactIdObjects).id= "EmailId"+iContactCount; 也是可以的 |
|
|
2楼#
发布于:2008-01-17 22:32
代码太长,没看完。感觉好像这2行有点问题
range.setStartBefore(el); frag = range.createContextualFragment(html); |
|
|
3楼#
发布于:2008-01-17 22:32
不记得有getElementsByName。
|
|
