javascript关于onclick()

1

<html>
<head>
<title>js1
</title>
<style>
#content{
margin:0 auto;
text-align:center;
}
</style>
</head>
<body onload="alert('hello!');">
<div id=content>
<img src="stone.jpg"/>
</div>
</body>
</html>

2

<html>
<head>
<title>js1
</title>
<style>
#content{
margin:0 auto;
text-align:center;
}
#rockImg{
cursor:pointer;}
</style>
<script>
function touchRock(){
var userName=prompt("你的名字是什么?");
if(userName)
{
alert("很高兴遇见你"+userName+"!");
document.getElementById("rockImg").src="rock_stone.jpg";
}}
</script>
</head>
<body>
<div id=content>
<img id="rockImg" src="stone.jpg" onclick="touchRock()"/>
</div>
</body>
</html>

脚本通常以以下几种方式:文本/数字/布尔

区分常量(const)和变量(var)

原文地址:https://www.cnblogs.com/Yimi/p/7642143.html