gpt4 book ai didi

JavaScript 代码仅适用于 IE

转载 作者:行者123 更新时间:2023-11-30 18:03:42 25 4
gpt4 key购买 nike

下面是创建一行新文本字段的代码,假设该行指定了 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 is not a function [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);
}

Uncaught TypeError: Object # has no method '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(或任何其他风格)后,确保您正在处理元素节点

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 代码仅适用于 IE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16298036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com