所以基本上我不知道为什么在我调用add()函数时(当您单击添加按钮时)给我以下错误...

它说:status.style未定义
状态是一个html dom,一个html标签

我认为错误几乎位于表格的末尾



var myArray = []; // * used to store all the data
myArray[0] = ['John', 'Doe', '1980'];
myArray[1] = ['Jane','Malloy','1982'];
myArray[2] = ['Vincent','Malloy','1988'];

var firstName = document.getElementById('firstName');
var secondName = document.getElementById('secondName');
var bornYear = document.getElementById('bornYear');
var output = document.getElementById('output');
var form1 = document.getElementById('form1');
var status = document.getElementById('status');

var add = function() { //calls statusMessagge()

	// check if input[] its not empty...
	if ( firstName.value.length>0 && secondName.value.length>0 && bornYear.value.length>0 ) {
		// * adding inputs to myArray
		myArray[myArray.length] = [firstName.value ,secondName.value ,bornYear.value ];
		//clearBoxes();
		// * status messagge
		statusMessagge('good');
		alert('is good');
	}
	else {
		statusMessagge('bad');
		alert('is bad');
	}
};

var statusMessagge = function(arg) { // * it is been called by: add(), show()

	// * selecting the messagge to appear
	switch (arg) {
		case 'good':
			status.innerHTML = 'Person added successfully.';
			break;
		case 'bad':
			status.innerHTML = 'Please fill all the fields.';
			break;
		case 'loading':
			status.innerHTML = 'Loading...';
			break;
		case 'loaded':
			status.innerHTML = 'Finish.';
			break;
	}

	// * do opacity effect slow: show|hide
	status.style.opacity = 1; // this is the part that I get the error.
	setTimeout (function() {
		status.removeAttribute('style');
	}, 1000);
};

body {
	background: lightgray;
	font-family: consolas;
	font-size: 13px;
	padding: 0;
	margin: 0;
}
main {
	background: #dbcdcd;
	margin: 0 auto;
}
form:nth-of-type(1) {
	float: left;
}
form:nth-of-type(2) {
	float: left;
}
label { /* for alining elements correctly */
	display: inline-block;
	width: 77px;
	text-align: right;
}
input[type="text"]:not(:first-of-type) {
	margin-top: 5px;
}
#status {
	opacity: 0;
	transition: opacity .20s;
	clear: both;
}

<!doctype html>
<html lang="es-ES">
	<head>
		<title>.:_My Exercise_:.</title>
		<link rel="stylesheet" type="text/css" href="style.css"/>
		<meta charset="utf-8"/>
	</head>
	<body>
		<main>
			<form id="form1" action=""> <!--action="#"  onsubmit="return false"-->
				<fieldset>
				<legend>Please introduce new person...</legend>

					<label>firstName:</label>
					<input id="firstName" type="text" autofocus tabindex="1"/>
					<input type="button" value="Add" onclick="add()"/> <br/>

					<label>secondName:</label>
					<input id="secondName" type="text" tabindex="2"/>
					<input type="button" value="Show" onclick="show()"/> <br/>

					<label>bornYear:</label>
					<input id="bornYear" type="text" tabindex="3"/>
					<input type="button" value="Sort" onclick="sort()"/>
				</fieldset>
			</form>

			<form>
				<fieldset>
				<legend>Sort...</legend>
					<input type="button" value="a-z" onclick=""/>
					<input type="button" value="z-a" onclick=""/>
				</fieldset>
			</form>

			<p id="status"></p>

			<p id="output"></p>
		</main>
		<script src="script.js"></script>
	</body>
</html>

最佳答案

我认为您想要的是在控制台中显示一条消息。为此使用console.log()。它对我来说在Firefox中起作用。

例:

// ... (your previous code)
if ( firstName.value.length>0 && secondName.value.length>0 && bornYear.value.length>0 ) {

    myArray[myArray.length] = [firstName.value ,secondName.value ,bornYear.value ];

    console.log("good");
    alert('is good');
}
else {
    console.log("bad");
    alert('is bad');
}
// ...

10-06 00:15