嗨,我是 Javascript 新手,我正在尝试获取一个函数,以便在窗口加载后删除一些元素。
这是HTML
<html>
<head>
<title>Simple Quiz</title>
<script type="text/javascript" src="script.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Quiz</h1>
<h2>Question</h2>
<p id="question"></p>
<form name="choices">
<fieldset>
<!-- insert questions here dynamically via javascript! -->
<label><input type="radio" name="choice" value="1st">First-class</label>
<label><input type="radio" name="choice" value="2day">2-day Air</label>
<label><input type="radio" name="choice" value="overnite">Overnight</label>
</fieldset>
</form>
</body>
</html>
页面加载后在控制台中调用函数 removeQuestions() 工作正常,但我似乎无法使用窗口的 onload 事件使其工作。有趣的是,它适用于 JSFiddle(在此处找到)但当我在 chrome 中本地启动页面时不起作用。我做错了什么?谢谢
这是 script.js 文件:
// JS executed after window has been loaded
function removeQuestions() {
var choices = document.forms.choices.elements[0].children;
// remove choices
while (choices.length > 0) {
var choice = choices[0];
choice.parentNode.removeChild(choice);
}
}
window.onload = removeQuestions();