我有這段代碼,點擊按鈕「獲取更多字段」將創建整個div的副本並創建字段。但是在點擊按鈕時沒有任何事情發生。所有其他按鈕工作正常。不知道我做錯了什麼。 感謝您的幫助提前。輸入類型的onClick函數=「按鈕」不起作用
<html>
<head>
</head>
<body>
<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="index1.php">
<span id="writeroot"></span>
<input type="button" id="moreFields" value="Give me more fields!" />
<input type="submit" value="Send form" />
</form>
</body>
</html>
<script>
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>
在chrome中打開它,右鍵單擊頁面上的任意位置 - >檢查元素,並在打開面板的右下方檢查紅色「x」並單擊它。有沒有錯誤?如果是的話,在這裏分享這些錯誤 – pythonian29033
檢查之前的錯誤。沒有錯誤顯示 – kirk
把你的腳本標籤放在body標籤內 – pythonian29033