我创建了一个带有按钮的HTML页面,显示一条消息:"这是一条消息"。一切都很好,但是当我按下按钮时,不会收到消息。我检查过任何错误,但我找不到任何错误。我做错了什么?
<!DOCTYPE html>
<html>
<head>
<title>Testing JavaScript</title>
<link rel="stylesheet" type="css" href="Button.css">
</head>
<body>
<h1>Javascript</h1>
<p>I am testing JS on this page.<br/>
This is a button.</p>
<script>
var message = "This is a message";
function popUp()
{
alert(message);
}
</script>
<button type="button" onclick="press()">Press me!</button>
</body>
</html>
答案 0 :(得分:3)
您应该致电onclick="popUp()"
答案 1 :(得分:2)
而不是struct Pond : Storable {
static let hash = "Pond"
let pk: Int
let depth: Int
}
struct Frog {
let pk: Int
let name: String
let pondPk: Int?
weak var store: Store? // reference to central Store()
var pond: Pond? {
guard let pondPk = pondPk else { return nil }
return store?.fetch(Pond.self, withPk: pondPk) as? Pond
}
}
:
func printPondDepth(frog: Frog) {
if let _ = frog.pondPk {
print(frog.pond?.depth ?? "No pond with id \(frog.pondPk ?? 0) in frog \(frog.name):s associated store")
}
else {
print("No pond id associated with frog \(frog.name)")
}
}
/* Central store */
let store = Store()
/* Some frogs read prior to any Ponds being added to the store */
let frog = Frog(pk: 1, name: "Kermit", pondPk: 1, store: store)
let anotherFrog = Frog(pk: 2, name: "Mitker", pondPk: 3, store: store)
/* Adding ponds to the store */
store.add(Pond(pk: 1, depth: 4))
store.add(Pond(pk: 4, depth: 25))
/* another frog and some attempts to get the depth of the
pond in which a frog possibly resides */
let thirdFrog = Frog(pk: 3, name: "Kerker", pondPk: nil, store: store)
printPondDepth(frog) /* 4 */
printPondDepth(anotherFrog) /* No pond with id 3 in frog Mitker:s associated store */
printPondDepth(thirdFrog) /* No pond id associated with frog Kerker */
答案 2 :(得分:1)
您没有声明名为“press()”的函数,更改代码
<!DOCTYPE html>
<html>
<head>
<title>Testing JavaScript</title>
<link rel="stylesheet" type="css" href="Button.css">
</head>
<body>
<h1>Javascript</h1>
<p>I am testing JS on this page.<br/>
This is a button.</p>
<script>
var message = "This is a message";
function popUp()
{
alert(message);
}
</script>
<button type="button" onclick="popUp()">Press me!</button>
</body>
</html>