1

下面是创建新文本字段行的代码,如果该行指定了 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 This Error]

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);  
}

未捕获的类型错误:对象 # 没有方法 'insertAdjacentHTML'

另外我在 Chrome 中再次检查,它给了我错误:when addRow is called

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>
4

1 回答 1

2

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

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

既然是文本节点,不是元素节点,那当然就没有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...
    }
}

正如您可能已经猜到的那样,另一端将涉及firstChildand nextSibling

于 2013-04-30T11:08:55.463 回答