JavaScript代码仅适用于IE

时间:2013-04-30 10:48:22

标签: javascript jquery

下面是创建新行文本字段的代码,例如,如果行指定了4个文本字段,下面的代码在单击[添加]按钮时创建与上面相同的附加行。

以下代码仅适用于IE,但在任何其他浏览器中均无效:

var delName = "ID=oth4del";
var t = oth4;
t.str = "\r\n      <td><table class=\"query\" width=\"100%\">\r\n          <tr>\r\n            <td><input type=\"text\" name=\"q269r1491i~\" size=\"22\" maxlength=\"50\">\r\n            </td>\r\n            <td><input type=\"text\" name=\"q270r1447i~\" size=\"22\" maxlength=\"50\">\r\n            </td>\r\n            <td><input type=\"text\" name=\"q2765r6758i~\" size=\"22\" maxlength=\"50\">\r\n              </font></td>\r\n          </tr>\r\n        </table></td>\r\n      ";
t.str = '<table width="100%"><tr chooserroot="true"><td><input type="button" value="Del" onclick="delrow(this)" '+delName+'></td>'+t.str+'</tr></table>';
t.cnt = 1;
document.multis.push(t);

我得到的错误是:

  

TypeError:m.lastChild.insertAdjacentHTML不是函数[Break On   这个错误]

m.lastChild.insertAdjacentHTML('beforeBegin', m.str.replace(/~/g,++m.cnt));

添加按钮代码:

调用以下函数:

<table align="center"><tr><td><input type="button" value="Add" onclick="addrow(this)" ID="insurance1Add"
></td></tr></table>


document.multis = new Array();

function findparent(e,tn) {
  while(e && e.parentNode && e.tagName && e.tagName.toLowerCase()!=tn) e = e.parentNode;
  return e;
}

function addrow(b) 
{
  var m = findparent(b,"div");  
  m.lastChild.insertAdjacentHTML('beforeBegin', m.str.replace(/~/g,++m.cnt));    
 }
///////////////////////////////////DEL ROW
function delrow(b)
{
  var table = findparent(b,"table");
  //var t = findparent(b,"div");
  //alert(t.cnt);
  //alert(table.cnt);
  table.parentNode.removeChild(table);  
}
  

未捕获TypeError:Object#没有方法'insertAdjacentHTML'

我还在Chrome中再次检查过,它给我发错误:调用addRow时

HTML生成的代码:

<DIV ID="Div_Insurance" >
        <SPAN id="INSURANCE" style="display:inline;">


<div ID="insurance1"
 >


<table border="1%" align="center" width="100%" style="display:none"><tr>
      <td><table class="query" width="100%">
          <tr>
            <td >Type</td>
            <td><select name="q2485i~">
                <option value="5581">Pharmaceutical</option>
                <option value="5582" selected>Long Term Care</option>
                <option value="5583">Home Care</option>
                <option value="5584">Other</option>
              </select>
            </td>
            <td>Other</td>
            <td><input type="text" name="q2764r6757i~" placeholder="Other Infomation" size="22" maxlength="50"></td>
          </tr>
          <tr>
            <td>Name of Insurance</td>
            <td><input type="text" name="q2438r5533i~" placeholder="Insurance Name" size="22" maxlength="50">
            </td>
            <td>Contact Person</td>
            <td><input type="text" name="q2439r5534i~" placeholder="Contact Person" size="22" maxlength="50">
            </td>
          </tr>
          <tr>
            <td>Address</td>
            <td><textarea cols="20" rows="4"  maxlength="50" name="q2440r5535i~" placeholder="Address"></textarea>
            </td>
            <td>Phone</td>
            <td><input type="text" name="q2441r5536i~" size="22" maxlength="50" onKeyUp="jm_phonemask(this)" onBlur="wphoneLength(this)"; placeholder="XXX-XXX-XXXX">
            </td>
          </tr>
          <tr>
            <td>Fax</td>
            <td><input type="text" name="q2442r5537i~" size="22" maxlength="50" onKeyUp="jm_phonemask(this)" onBlur="wphoneLength(this)"; placeholder="XXX-XXX-XXXX">
            </td>
            <td>Email</td>
            <td><input type="text" name="q2443r5538i~" size="22" maxlength="50" placeholder="xyz@xyz.xyz">
            </td>
          </tr>
        </table></td>
      </tr></table>

<table align="center"><tr><td><input type="button" value="Add" onclick="addrow(this)" ID="insurance1Add"
></td></tr></table>

</div>

<script>
var delName = "ID=insurance1del";
var t = insurance1;
t.str = "\r\n      <td><table class=\"query\" width=\"100%\">\r\n          <tr>\r\n            <td >Type</td>\r\n            <td><select name=\"q2485i~\">\r\n                <option value=\"5581\">Pharmaceutical</option>\r\n                <option value=\"5582\" selected>Long Term Care</option>\r\n                <option value=\"5583\">Home Care</option>\r\n                <option value=\"5584\">Other</option>\r\n              </select>\r\n            </td>\r\n            <td>Other</td>\r\n            <td><input type=\"text\" name=\"q2764r6757i~\" placeholder=\"Other Infomation\" size=\"22\" maxlength=\"50\"></td>\r\n          </tr>\r\n          <tr>\r\n            <td>Name of Insurance</td>\r\n            <td><input type=\"text\" name=\"q2438r5533i~\" placeholder=\"Insurance Name\" size=\"22\" maxlength=\"50\">\r\n            </td>\r\n            <td>Contact Person</td>\r\n            <td><input type=\"text\" name=\"q2439r5534i~\" placeholder=\"Contact Person\" size=\"22\" maxlength=\"50\">\r\n            </td>\r\n          </tr>\r\n          <tr>\r\n            <td>Address</td>\r\n            <td><textarea cols=\"20\" rows=\"4\"  maxlength=\"50\" name=\"q2440r5535i~\" placeholder=\"Address\"></textarea>\r\n            </td>\r\n            <td>Phone</td>\r\n            <td><input type=\"text\" name=\"q2441r5536i~\" size=\"22\" maxlength=\"50\" onKeyUp=\"jm_phonemask(this)\" onBlur=\"wphoneLength(this)\"; placeholder=\"XXX-XXX-XXXX\">\r\n            </td>\r\n          </tr>\r\n          <tr>\r\n            <td>Fax</td>\r\n            <td><input type=\"text\" name=\"q2442r5537i~\" size=\"22\" maxlength=\"50\" onKeyUp=\"jm_phonemask(this)\" onBlur=\"wphoneLength(this)\"; placeholder=\"XXX-XXX-XXXX\">\r\n            </td>\r\n            <td>Email</td>\r\n            <td><input type=\"text\" name=\"q2443r5538i~\" size=\"22\" maxlength=\"50\" placeholder=\"xyz@xyz.xyz\">\r\n            </td>\r\n          </tr>\r\n        </table></td>\r\n      ";
t.str = '<table width="100%"><tr chooserroot="true"><td><input type="button" value="Del" onclick="delrow(this)" '+delName+'></td>'+t.str+'</tr></table>';
t.cnt = 1;
document.multis.push(t);
</script>


      </SPAN>
      </DIV>

1 个答案:

答案 0 :(得分:2)

addrow()函数中,执行console.log(m.lastChild)并查看它是什么。

检查其nodeType。我怀疑它是一个文本节点(即nodeType = 3)。

由于它是一个文本节点,不是一个元素节点,当然它不会有insertAdjacentHTML方法。

参考文献:


这是您可能想要保留的经典旧学校片段。获得lastChild(或任何其他版本)后,请确保您正在使用元素节点

function addrow(b) {
    var m = findparent(b,"div"),
        lastChildElement = m.lastChild;

    // the old-school routine
    while (lastChildElement && lastChildElement.nodeType !== 1) {
        lastChildElement = lastChildElement.previousSibling;
    }

    if (lastChildElement) {
        lastChildElement.insertAdjacentHTML('beforeBegin', m.str.replace(/~/g,++m.cnt));    
    } else {
        // handle this case...
    }
}

正如您可能已经猜到的那样,另一端会涉及firstChildnextSibling