我是 JavaScript DOM 的新手。我在www.quirksmode.org/dom/domform.html找到了一个教程。我重写了代码以满足我的需要,但是当我无法让它工作时,我将代码从这个页面直接复制到了一个测试页面中。moreFields 函数将运行 onload,但不会运行 onclick。removeFields 函数确实有效。我已经在 Firefox 和 Chromium 中测试了代码。它在教程中的 Firefox 中有效,但即使我逐字复制它也无效。我什至尝试从教程中复制源代码。
<script type="text/javascript">
<!--
var counter = 0;
function moreFields() {
counter++;
var newFields = document.getElementById(\'readroot\').cloneNode(true);
newFields.id = \'\';
newFields.style.display = \'block\';
var newField = newFields.childNodes;
for (var i=0;i<newField.length;i++) {
var theName = newField[i].name
if (theName)
newField[i].name = theName + counter;
}
var insertHere = document.getElementById(\'writeroot\');
insertHere.parentNode.insertBefore(newFields,insertHere);
}
window.onload = moreFields;
// -->
</script>
<div id="readroot" style="display: none">
<input type="button" value="Remove review"
onclick="this.parentNode.parentNode.removeChild(this.parentNode);" /><br /><br />
<input name="cd" value="title" />
<select name="rankingsel">
<option>Rating</option>
<option value="excellent">Excellent</option>
<option value="good">Good</option>
<option value="ok">OK</option>
<option value="poor">Poor</option>
<option value="bad">Bad</option>
</select><br /><br />
<textarea rows="5" cols="20" name="review">Short review</textarea>
<br />Radio buttons included to test them in Explorer:<br />
<input type="radio" name="something" value="test1" />Test 1<br />
<input type="radio" name="something" value="test2" />Test 2
</div>
<form method="post" action="/cgi-bin/show_params.cgi">
<span id="writeroot"></span>
<input type="button" id="moreFields" value="Give me more fields!" />
<input type="submit" value="Send form" />
</form>