下面的代码创建一行新的文本字段,例如,如果该行指定了4个文本字段,则当我单击[Add]按钮时,下面的代码将创建与上面相同的其他行。

以下代码仅在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:对象#没有方法'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>

最佳答案

在您的addrow()函数中,执行一个console.log(m.lastChild)并查看它的含义。

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

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

参考文献:


lastChild:https://developer.mozilla.org/en-US/docs/DOM/Node.lastChild
nodeType:https://developer.mozilla.org/en-US/docs/DOM/Node.nodeType
insertAdjacentHTML:https://developer.mozilla.org/en-US/docs/DOM/element.insertAdjacentHTML




这是您可能想要保留的经典的老式片段。获得lastChild(或其他任何样式)后,请确保您正在使用Element Node。

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

关于javascript - JavaScript代码仅在IE中有效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16298036/

10-10 18:41
查看更多